We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f779b6 commit 227601aCopy full SHA for 227601a
construct-binary-tree-from-preorder-and-inorder-traversal/sun912.py
@@ -0,0 +1,19 @@
1
+"""
2
+TC: O(n)
3
+SC: O(n)
4
5
+# Definition for a binary tree node.
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
+class Solution:
12
+ def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
13
+ if inorder:
14
+ idx = inorder.index(preorder.pop(0))
15
+ node = TreeNode(inorder[idx])
16
+ node.left = self.buildTree(preorder, inorder[0:idx])
17
+ node.right = self.buildTree(preorder, inorder[idx+1:])
18
+
19
+ return node
0 commit comments