- See Code
Complexity T : O(N) M : O(n)
# 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 buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
def build_tree(pre, i):
if not pre:
return None
node = TreeNode(pre[0])
index = i.index(pre[0])
node.left = build_tree(pre[1:1+index], i[:index])
node.right = build_tree(pre[1+index:], i[index+1:])
return node
return build_tree(preorder, inorder)