116. Populating Next Right Pointers in Each Node
Description
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:

- Input: root = [1,2,3,4,5,6,7]
- Output: [1,#,2,3,#,4,5,6,7,#]
- Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.
Example 2:
- Input: root = []
- Output: []
Constraints:
- The number of nodes in the tree is in the range [0, 212 - 1].
- -1000 <= Node.val <= 1000
Follow up:
- You may only use constant extra space.
- The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
Submitted Code
"""
# Definition for a Node.
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
"""
class Solution:
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
level_start = root # 레벨 시작(가장 왼쪽 노드)
while level_start and level_start.left:
head = level_start # 연결 리스트 시작
while head:
head.left.next = head.right # 같은 부모의 자식끼리 연결
if head.next: # 다른 부모의 자식끼리 연결
head.right.next = head.next.left
head = head.next
level_start = level_start.left
return root
Runtime: 48 ms | Beats 95.70%
Memory: 20.68 MB | Beats 48.31%
현재 레벨의 가장 왼쪽 노드를 가리키는 level_start 포인터와 현재 레벨을 연결 리스트처럼 가로로 순회하는 head 포인터를 사용해서 자식들의 next를 연결했다.
Other Solutions
1st
class Solution:
def connect(self, root):
if not root: return None
L, R, N = root.left, root.right, root.next
if L:
L.next = R
if N: R.next = N.left
self.connect(L)
self.connect(R)
return root
time complexity: 𝑂(𝑛)
space complexity: 𝑂(1) → 재귀 스택 제외
재귀 호출로 구현한 코드이다.