FunctionRacerProblemsRace

Build Tree from Inorder and Postorder

MediumTreesPython, JavaScript, TypeScript, Java, C++

You get the lists inorder and postorder, the traversals of one binary tree with distinct values. Rebuild the tree and return its root. (The last item of the postorder list is the root.)

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

inorder [9,3,15,20,7]
postorder [9,15,7,20,3]
returns [3,9,20,null,null,15,7]

Root 3; left subtree 9; right subtree 20 with children 15 and 7.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems