A tree diagram is drawn on a grid where every level has room for a full set of slots: level d has 2^d slots, and each node's children take the two slots below it. Empty slots between nodes still take space.
Write widestRow(root) that returns the largest width of any level, where a level's width is the number of slots from its leftmost node to its rightmost node, counting both and every slot between them. An empty tree returns 0.
Input: root = [1,2,3,4,null,null,5] Output: 4
On level 2, 4 is in slot 0 and 5 in slot 3, so the width is 4.
Input: root = [1,2,3,4] Output: 2
Level 1 has width 2. Level 2 has only 4, width 1.
0 <= number of nodes <= 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 #328 Widest Row Including Gaps. 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.