Skip to content

Commit 81c294e

Browse files
authoredMar 14, 2022
Create 105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal.py
1 parent 7372506 commit 81c294e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
3+
if not preorder or not inorder:
4+
return None
5+
6+
root = TreeNode(preorder[0])
7+
mid = inorder.index(preorder[0])
8+
root.left = self.buildTree(preorder[1:mid + 1], inorder[:mid])
9+
root.right = self.buildTree(preorder[mid + 1:], inorder[mid + 1:])
10+
return root

0 commit comments

Comments
 (0)