Problem Statement:
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.
Examples:
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
Approach
- Use DFS (Depth-First Search) to connect nodes.
- For each node:
- Set
left.next = right. - If
nextexists, connectright.next = next.left.
- Set
- Recursively repeat for left and right children.
Time Complexity:
Time Complexity = O(n * m)
Space Complexity:
Space Complexity = O(log n) recursion stack space (h=tree height)
Dry Run
root = Node(1)
├── left: Node(2)
│ ├── left: Node(4)
│ └── right: Node(5)
└── right: Node(3)
├── left: Node(6)
└── right: Node(7)
Step 1:
→ Call traversal(curr = root = 1)
→ curr.left = 2 → set 2.next = 3
→ curr.right = 3 and curr.next = null → skip setting 3.next
→ Call traversal(curr = 2)
→ curr.left = 4 → set 4.next = 5
→ curr.right = 5 and curr.next = 3 → set 5.next = 3.left = 6
→ Call traversal(curr = 4)
→ curr.left = null → skip
→ curr.right = null → skip
→ Return
→ Call traversal(curr = 5)
→ curr.left = null → skip
→ curr.right = null → skip
→ Return
→ Return from traversal(curr = 2)
→ Call traversal(curr = 3)
→ curr.left = 6 → set 6.next = 7
→ curr.right = 7 and curr.next = null → skip setting 7.next
→ Call traversal(curr = 6)
→ curr.left = null → skip
→ curr.right = null → skip
→ Return
→ Call traversal(curr = 7)
→ curr.left = null → skip
→ curr.right = null → skip
→ Return
→ Return from traversal(curr = 3)
Final Result: The tree is modified such that each node's next pointer points to its adjacent node on the same level.
var connect = function(root) {
if(!root) return root;
let traversal = (curr) => {
if(curr.left) {
curr.left.next = curr.right;
}
if(curr.right && curr.next) {
curr.right.next = curr.next.left;
}
curr.left && traversal(curr.left);
curr.right && traversal(curr.right);
}
traversal(root);
return root;
};
class Node:
def __init__(self, val=0, left=None, right=None, next=None):
self.val = val
self.left = left
self.right = right
self.next = next
def connect(root):
if not root:
return root
def traversal(curr):
if curr.left:
curr.left.next = curr.right
if curr.right and curr.next:
curr.right.next = curr.next.left
if curr.left:
traversal(curr.left)
if curr.right:
traversal(curr.right)
traversal(root)
return root
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node(int val) {
this.val = val;
}
}
class Solution {
public Node connect(Node root) {
if (root == null) return null;
traverse(root);
return root;
}
private void traverse(Node curr) {
if (curr.left != null) {
curr.left.next = curr.right;
}
if (curr.right != null && curr.next != null) {
curr.right.next = curr.next.left;
}
if (curr.left != null) traverse(curr.left);
if (curr.right != null) traverse(curr.right);
}
}
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node(int _val) : val(_val), left(nullptr), right(nullptr), next(nullptr) {}
};
class Solution {
public:
Node* connect(Node* root) {
if (!root) return root;
auto traversal = [&](Node* curr, auto&& traversal_ref) -> void {
if (curr->left) {
curr->left->next = curr->right;
}
if (curr->right && curr->next) {
curr->right->next = curr->next->left;
}
if (curr->left) traversal_ref(curr->left, traversal_ref);
if (curr->right) traversal_ref(curr->right, traversal_ref);
};
traversal(root, traversal);
return root;
}
};
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
struct Node* next;
} Node;
void traversal(Node* curr) {
if (!curr) return;
if (curr->left) {
curr->left->next = curr->right;
}
if (curr->right && curr->next) {
curr->right->next = curr->next->left;
}
if (curr->left) traversal(curr->left);
if (curr->right) traversal(curr->right);
}
Node* connect(Node* root) {
if (!root) return NULL;
traversal(root);
return root;
}
public class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node(int val = 0, Node left = null, Node right = null, Node next = null) {
this.val = val;
this.left = left;
this.right = right;
this.next = next;
}
}
public class Solution {
public Node Connect(Node root) {
if (root == null) return root;
void Traverse(Node curr) {
if (curr.left != null) {
curr.left.next = curr.right;
}
if (curr.right != null && curr.next != null) {
curr.right.next = curr.next.left;
}
if (curr.left != null) Traverse(curr.left);
if (curr.right != null) Traverse(curr.right);
}
Traverse(root);
return root;
}
}
