Build Tree from Inorder and Postorder
Rebuild a binary tree from its inorder and postorder traversals.
The problem
Implement Solution.buildTree(inorder, postorder). inorder and postorder are the inorder and postorder 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, inorder = [9, 3, 15, 20, 7] and postorder = [9, 15, 7, 20, 3] 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, inorder, postorder):
passPublic examples
inorder = [9, 3, 15, 20, 7], postorder = [9, 15, 7, 20, 3]gives[3, 9, 20, None, None, 15, 7]inorder = [-8], postorder = [-8]gives[-8]inorder = [1, 2], postorder = [2, 1]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.
