Skip to content

Commit eee5afc

Browse files
committed
added Construct Binary Tree Given pre&inorder traversals (medium)
1 parent 2f628e0 commit eee5afc

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Construct Binary Tree from Preorder and Inorder Traversal
2+
3+
[Leetcode Link](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
4+
5+
## Problem:
6+
7+
Given preorder and inorder traversal of a tree, construct the binary tree.
8+
9+
## Example:
10+
11+
```
12+
Given:
13+
preorder = [3,9,20,15,7]
14+
inorder = [9,3,15,20,7]
15+
16+
Returns following binary tree:
17+
3
18+
/ \
19+
9 20
20+
/ \
21+
15 7
22+
```
23+
24+
## Note:
25+
26+
You may assume that duplicates do not exist in the tree.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from typing import List
2+
3+
# Definition for a binary tree node.
4+
5+
6+
class TreeNode:
7+
def __init__(self, val=0, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
def __str__(self):
13+
tree = list()
14+
queue = list()
15+
queue.append(self)
16+
while queue and not all(node is None for node in queue):
17+
currentNode = queue.pop(0)
18+
if currentNode is None:
19+
tree.append(None)
20+
else:
21+
tree.append(currentNode.val)
22+
queue.append(currentNode.left)
23+
queue.append(currentNode.right)
24+
return str(tree)
25+
26+
27+
class Solution:
28+
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
29+
return self.recursiveBuild(0, 0, len(inorder), preorder, inorder)
30+
31+
def recursiveBuild(self, preorderIndex: int, inorderStart: int, inorderEnd: int, preorder: List[int], inorder: List[int]) -> TreeNode:
32+
if inorderStart >= inorderEnd or preorderIndex >= len(preorder):
33+
return None
34+
node = TreeNode(preorder[preorderIndex])
35+
# find index of this node value at inorder
36+
inorderIndex = inorder.index(preorder[preorderIndex])
37+
# left node of current node is always next element in preorder (also pass in left subarray range)
38+
node.left = self.recursiveBuild(
39+
preorderIndex+1, inorderStart, inorderIndex, preorder, inorder)
40+
# right node of current node is current node + num of all left nodes + 1
41+
numLeftNodes = inorderIndex - inorderStart
42+
node.right = self.recursiveBuild(
43+
preorderIndex+numLeftNodes+1, inorderIndex+1, inorderEnd, preorder, inorder)
44+
return node
45+
46+
47+
sol = Solution()
48+
preorder = [3, 9, 20, 15, 7]
49+
inorder = [9, 3, 15, 20, 7]
50+
51+
root = sol.buildTree(preorder, inorder)
52+
53+
print(root)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Languages used: Java and Python
8383
- [Divide Array is Sets of K Consecutive Numbers](Medium/DivideArrayInKConsecNums)
8484
- [Perfect Squares](Medium/PerfectSquares)
8585
- [Score After Flipping Matrix](Medium/ScoreAfterFlippingMatrix)
86+
- [Construct Binary Tree from Preorder and Inorder Traversal](Medium/ConstructBinaryTree)
8687
- Hard
8788
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8889
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)