-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConnectAllSiblings.java
More file actions
37 lines (34 loc) · 953 Bytes
/
ConnectAllSiblings.java
File metadata and controls
37 lines (34 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
Pattern: Tree BFS
08 Connect All Level Order Siblings (medium)
Given a binary tree, connect each node with its level order successor.
The last node of each level should point to the first node of the next level.
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode next;
TreeNode(int x) {
val = x;
left = right = next = null;
}
};
class ConnectAllSiblings {
public void connect(TreeNode root) {
if(root==null || root.left == null && root.right == null) return;
IterativeBFS(root);
}
private void IterativeBFS(TreeNode root){
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode curr = queue.poll();
if(curr.left != null) queue.offer(curr.left);
if(curr.right != null) queue.offer(curr.right);
if(queue.peek() != null){
curr.next = queue.peek();
} else curr.next = null;
}
}
}