Your editor saves its split-pane layout to localStorage as one string. The format is a preorder walk (node, left subtree, right subtree) where each node writes its value, each missing child writes #, and items are separated by commas.
Write writeLayout(root) that returns that string. For example, a root 1 with only a left child 2 is written 1,2,#,#,#. An empty tree is written #.
Input: root = [1,2,3,null,null,4,5] Output: "1,2,#,#,3,4,#,#,5,#,#"
After 2 come its two empty children, then 1's right subtree.
Input: root = [] Output: "#"
An empty tree is a single #.
0 <= number of nodes <= 10^4Trees 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 #362 Write a Layout as Text. 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.