-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct Binary Tree From Preorder and Inorder Traversal.py
48 lines (34 loc) · 1.72 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
44
45
46
47
48
# https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/submissions/
# 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
class Solution:
def recursiveCall(self, preorder, inorder):
# NOTE: The 1st element in preorder will always be root
# When you find the root from preorder, you need to also find it in inorder because once found you can tell what elements
# are in the left and right subtree
# If there aren't any more elements, exit function
# NOTE: I think "inorder" was used here because the last element will be a leaf on the far-right subtree
if len(inorder) == 0:
return None
# Pop 1st element in preorder
rootValue = preorder.pop(0)
# Create a root node
root = TreeNode(rootValue)
# Find root in "inorder"
inorderIdx = inorder.index(rootValue)
# Get left and right subtree
root.left = self.recursiveCall(preorder, inorder[:inorderIdx])
root.right = self.recursiveCall(preorder, inorder[inorderIdx + 1:])
# Return root node
return root
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
# Preorder: root, left, right
# Inorder: left, root, right
# If both lists only have 1 element, return a single node
if len(preorder) == 1 and len(inorder) == 1:
return TreeNode(preorder[0])
return self.recursiveCall(preorder, inorder)