Skip to content

Commit fda6438

Browse files
committed
feat: 105. Construct Binary Tree from Preorder and Inorder Traveral
1 parent c9c25a2 commit fda6438

File tree

1 file changed

+12
-0
lines changed
  • construct-binary-tree-from-preorder-and-inorder-traversal

1 file changed

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

0 commit comments

Comments
 (0)