Tap here to switch tabs
Problem
Submissions

Problem: Populating Next Right Pointers in Each Node II

med
30 min
Try to solve the Populating Next Right Pointers in Each Node II problem.

Statement

Given the root of a binary tree root, each node contains pointers left, right, and an additional pointer next. Populate every node’s next pointer so that it points to the node immediately to its right on the same level. If there is no node to the right on that level, set next to null.

Return the root root after updating all next pointers.

Note: You must use only constant extra space. A recursive solution is allowed, and the implicit recursion stack does not count as extra space.

Constraints:

  • The number of nodes in the tree is in the range [0,6000][0, 6000]

  • −100≤-100 \leq Node.val ≤100\leq 100

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Populating Next Right Pointers in Each Node II

med
30 min
Try to solve the Populating Next Right Pointers in Each Node II problem.

Statement

Given the root of a binary tree root, each node contains pointers left, right, and an additional pointer next. Populate every node’s next pointer so that it points to the node immediately to its right on the same level. If there is no node to the right on that level, set next to null.

Return the root root after updating all next pointers.

Note: You must use only constant extra space. A recursive solution is allowed, and the implicit recursion stack does not count as extra space.

Constraints:

  • The number of nodes in the tree is in the range [0,6000][0, 6000]

  • −100≤-100 \leq Node.val ≤100\leq 100