Skip to content

Commit 3f7bdc5

Browse files
author
weiy
committed
construct binary tree from preorder and postorder traversal medium contest
1 parent b39c639 commit 3f7bdc5

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
"""
2+
Return any binary tree that matches the given preorder and postorder traversals.
3+
4+
Values in the traversals pre and post are distinct positive integers.
5+
6+
7+
8+
Example 1:
9+
10+
Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1]
11+
Output: [1,2,3,4,5,6,7]
12+
13+
14+
Note:
15+
16+
1 <= pre.length == post.length <= 30
17+
pre[] and post[] are both permutations of 1, 2, ..., pre.length.
18+
It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.
19+
20+
根据二叉树的 前序和后序遍历,返回一颗完整的二叉树。
21+
22+
不唯一,返回随便一个即可。
23+
24+
思路:
25+
1. 二叉树的前序是 根左右。
26+
2. 二叉树的后序是 左右根。
27+
28+
在前序中确定 根 ,然后在后序中找左右子树。
29+
30+
pre = [1,2,4,5,3,6,7]
31+
post = [4,5,2,6,7,3,1]
32+
33+
总根是 1
34+
35+
1的左右其中一个是 2,就当它是左子树好了,因为是 根 左右 所以假设的话就先假设为左子树,如果只有一边的话,左右其实无所谓。
36+
37+
1
38+
/
39+
2
40+
41+
然后在 post 中找属于 2 这个子树的节点。
42+
找到 4 5 2,那么剩下的 6 7 3 就是与之相对的 1 的右子树了。
43+
44+
把 pre 分成 [2, 4, 5] [3, 6, 7]
45+
post 分为 [4, 5, 2] [6, 7 ,3]
46+
47+
这样 作为1的左右两颗子树已经出来了。
48+
pre[left] 和 pre[right] 的 0 分别为 左右两棵子树的根。
49+
50+
之后就是分别把左右两边的这两个代替原来的 pre post,根也同样代替,然后递归直到没有即可。
51+
52+
测试链接:
53+
https://leetcode.com/contest/weekly-contest-98/problems/construct-binary-tree-from-preorder-and-postorder-traversal/
54+
55+
beat 50% 40ms.
56+
57+
这应该是自己的极限了。
58+
4道题 一个半小时做 3 道题,1 easy 2 medium 0 hard.
59+
60+
61+
"""
62+
63+
# Definition for a binary tree node.
64+
# class TreeNode(object):
65+
# def __init__(self, x):
66+
# self.val = x
67+
# self.left = None
68+
# self.right = None
69+
70+
class Solution(object):
71+
def constructFromPrePost(self, pre, post):
72+
"""
73+
:type pre: List[int]
74+
:type post: List[int]
75+
:rtype: TreeNode
76+
"""
77+
78+
def getLeftAndRight(pre, post):
79+
# no more node.
80+
if not pre:
81+
return None
82+
83+
# Get the index of the left root.
84+
index = post.index(pre[0])
85+
86+
# post left tree
87+
val_children = post[:index+1]
88+
89+
# post right tree
90+
val_brother = post[index+1:-1]
91+
92+
# pre left tree
93+
# Get the left tree pre list
94+
# if left tree post list contains 3 elements
95+
# then we will get equal in pre list.
96+
t = len(val_children)
97+
pre_val_children = pre[:t]
98+
99+
# there is right tree.
100+
# The elements are the rest of pre list.
101+
pre_val_brother = pre[t:]
102+
103+
left = pre[0]
104+
105+
right = val_brother[-1] if val_brother else None
106+
107+
# left, right, pre, post
108+
return (left, right, pre_val_children, val_children, pre_val_brother, val_brother)
109+
110+
def construct(root, pre, post):
111+
x = getLeftAndRight(pre[1:], post)
112+
if root and x:
113+
if x[0] is not None:
114+
root.left = TreeNode(x[0])
115+
if x[1] is not None:
116+
root.right = TreeNode(x[1])
117+
118+
if root.left:
119+
construct(root.left, x[2], x[3])
120+
121+
if root.right:
122+
construct(root.right, x[4], x[5])
123+
124+
allRoot = TreeNode(pre[0])
125+
construct(allRoot, pre, post)
126+
127+
return allRoot
128+

0 commit comments

Comments
 (0)