A screen reader reads the panes of a layout in preorder: a pane, then everything in its left part, then everything in its right part. To make that easy, you want to turn the tree into a single chain in that order.
Write flattenLayout(root) that changes the tree in place: after it runs, every node's left is null, and following right pointers from the root visits the nodes in preorder. The function doesn't need to return anything.
Input: root = [4,2,7,1,3] Output: [4,null,2,null,1,null,3,null,7]
Preorder is 4, 2, 1, 3, 7, now linked by right pointers.
Input: root = [1,null,2] Output: [1,null,2]
It is already a chain.
0 <= number of nodes <= 2000Trees 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 #321 Flatten a Layout Into a Chain. 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.