Populating Next Right Pointers in Each Node II
MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.Each node of a binary tree has the usual val, left and right, plus a next pointer that starts as None (null). Given the root, set every node's next to the node immediately to its right on the same level, or leave it None if it is the last node on its level. The tree is not necessarily full: any child may be missing. Return the root.
Use only O(1) extra memory beyond the tree itself (no queue of nodes).
Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them. # marks the end of a level when you follow next pointers.
[10, 6, 14, 3, null, 12, 20] → 10 # 6 → 14 # 3 → 12 → 20 #
[1, 2, 3, 4, null, null, 5] → 1 # 2 → 3 # 4 → 5 #
[] → (empty)Constraints
0 <= number of nodes <= 6000-100 <= Node.val <= 100