A build system logs targets in postorder (left subtree, right subtree, then the target), because a target is built after its sub-targets. You also have the inorder listing (left, node, right) from the project file. Target ids are unique.
Write rebuildFromLog(inorder, postorder) that returns the root of the tree that produced both lists. Empty lists give an empty tree.
Input: inorder = [4,2,5,1,3,6], postorder = [4,5,2,6,3,1] Output: [1,2,3,4,5,null,6]
The last postorder value, 1, is the root.
Input: inorder = [2,1], postorder = [2,1] Output: [1,2]
1 is built last, so it is the root, and 2 is left of it in inorder.
0 <= n <= 3000Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #360 Rebuild a Tree From Its Build Log. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.