forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construct-binary-tree-from-preorder-and-inorder-traversal.py
43 lines (39 loc) · 1.34 KB
/
construct-binary-tree-from-preorder-and-inorder-traversal.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import print_function
# Time: O(n)
# Space: O(n)
#
# Given preorder and inorder traversal of a tree, construct the binary tree.
#
# Note:
# You may assume that duplicates do not exist in the tree.
#
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param preorder, a list of integers
# @param inorder, a list of integers
# @return a tree node
def buildTree(self, preorder, inorder):
lookup = {}
for i, num in enumerate(inorder):
lookup[num] = i
return self.buildTreeRecu(lookup, preorder, inorder, 0, 0, len(inorder))
def buildTreeRecu(self, lookup, preorder, inorder, pre_start, in_start, in_end):
if in_start == in_end:
return None
node = TreeNode(preorder[pre_start])
i = lookup[preorder[pre_start]]
node.left = self.buildTreeRecu(lookup, preorder, inorder, pre_start + 1, in_start, i)
node.right = self.buildTreeRecu(lookup, preorder, inorder, pre_start + 1 + i - in_start, i + 1, in_end)
return node
if __name__ == "__main__":
preorder = [1, 2, 3]
inorder = [2, 1, 3]
result = Solution().buildTree(preorder, inorder)
print(result.val)
print(result.left.val)
print(result.right.val)