A crash report captured two walks of the app's layout tree but not the tree itself: preorder (node, left, right) and inorder (left, node, right). All node values are unique.
Write rebuildLayout(preorder, inorder) that rebuilds the tree and returns its root. The two lists always describe the same valid tree. Empty lists give an empty tree.
Input: preorder = [1,2,4,5,3,6], inorder = [4,2,5,1,3,6] Output: [1,2,3,4,5,null,6]
1 is the root; [4, 2, 5] is left of it in inorder and [3, 6] is right.
Input: preorder = [7], inorder = [7] Output: [7]
A single node.
0 <= n <= 3000preorder and inorder hold the same unique values.Trees 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 #359 Rebuild a Layout From Two Walks. 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.