104. Populating Next Right Pointers in Each Node II
MediumTree
You are given a binary tree where each node has a next pointer, initially set to NULL. Populate each next pointer to point to its next right node on the same level; if there is no next right node, the next pointer should be set to NULL. Unlike the perfect-tree version, this tree can be any shape. Use only constant extra space (the implicit recursion stack does not count). This catalog task returns, for verification, each level's values read left-to-right by following the next pointers you set.
Examples
Input: [1,2,3,4,5,null,7]
Output: [[1],[2,3],[4,5,7]]
Explanation: Reading each level left-to-right via the next pointers yields 1 | 2 3 | 4 5 7.
Constraints
- The number of nodes in the tree is in the range [0, 6000]; -100 <= Node.val <= 100
Loading...
Run checks all cases above. Submit evaluates all test cases.