Build Tree from Preorder and Inorder
Rebuild a binary tree from its preorder and inorder traversals.
The problem
Implement Solution.buildTree(preorder, inorder). preorder and inorder are the preorder and inorder traversals of the same binary tree, and every value is unique. Rebuild that tree and return its root TreeNode (each node has val, left and right). If both lists are empty, return None. For example, preorder = [3, 9, 20, 15, 7] and inorder = [9, 3, 15, 20, 7] describe the tree [3, 9, 20, None, None, 15, 7] in level order, with None for a missing child. Constraints: both lists have the same length, from 0 to 3,000, and values are unique integers from -3,000 to 3,000. Do not change the input lists.
Starter code
class Solution:
def buildTree(self, preorder, inorder):
passPublic examples
preorder = [3, 9, 20, 15, 7], inorder = [9, 3, 15, 20, 7]gives[3, 9, 20, None, None, 15, 7]preorder = [-8], inorder = [-8]gives[-8]preorder = [1, 2], inorder = [1, 2]gives[1, None, 2]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
