-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTrees_105_constructFromPreOrderAndInOrderTraversal.py
65 lines (51 loc) · 2.18 KB
/
binaryTrees_105_constructFromPreOrderAndInOrderTraversal.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
https://www.youtube.com/watch?v=ihj4IQGZ2zc&list=PLot-Xpze53lfOdF3KwpMSFEyfE77zIwiP&index=24
leetcode 105
medium
input : two integer arrays preorder and inorder where preorder is the preorder traversal of
a binary tree and inorder is the inorder traversal of the same tree
output: construct and return the binary tree.
Logic :
-deconstruct pre and inorder traversals and reconstruct the binary tree
-eg : binary tree :
3
9 20
15 7
-preorder : root - left subtree - right subtree : [3,9,20,15,7]
-inorder : left subtree - root - right subtree : [9,3,15,20,7]
reconstruction :
- 1st value of preorder = root -> from preorder
- what will go into left subtree and what into right subtree -> from inorder
- each value in each traversal = unique
-identify root : 1st node in preorder
-remove root from preorder and inorder
-everything before root in inorder will go into left subtree, after into right subtree
-repeat recursively
-since single value 9 in left subtree, since done with left subtree, go to right subtree
-go back to remaining preorder = [20,15,7] since 3 and then 9 removed
-again 20 is the root from preorder
-from inorder to the left of 20 : 15 is in the left subtree and to the right : 7 : right subtree
-done when preorder and inorder arrays become empty
Time Complexity: o(n) and space : o(n)
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def buildTree(preorder, inorder):
# base case
if not preorder or not inorder:
return None
# root is 1st value in preorder
root = TreeNode(preorder[0])
# split at root in inorder
mid = inorder.index(preorder[0])
# create left and right subtrees recursively
root.left = buildTree(preorder[1 : mid + 1], inorder[:mid]) #pass sublists : new preorder and inorder arrays
root.right = buildTree(preorder[mid + 1 :], inorder[mid + 1 :])
return root
result = buildTree([3,9,20,15,7], [9,3,15,20,7])
print(result)