-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path前序中序求后序.py
More file actions
30 lines (26 loc) · 776 Bytes
/
Copy path前序中序求后序.py
File metadata and controls
30 lines (26 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# coding: utf8
# @Author: 郭 璞
# @File: 二叉树中和为某一个值的路径.py
# @Time: 2017/4/16
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 根据前序遍历序列,中序遍历序列重建一棵二叉树
class Node(object):
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
def rebuild(pre, center):
if not pre:
return
cur = Node(pre[0])
index = center.index(pre[0])
cur.left = rebuild(pre[1:index + 1], center[:index])
cur.right = rebuild(pre[index + 1:], center[index + 1:])
return cur
def deep(root):
if not root:
return
deep(root.left)
deep(root.right)
print (root.data)