-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPopulateNextRight2.java
61 lines (58 loc) · 1.78 KB
/
PopulateNextRight2.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package algorithm.basic.iterativetree;
/**
* You are given a tree:
* class 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.
*/
public class PopulateNextRight2 {
public Node connect2(Node root) {
if (root == null) {
return null;
}
Node leftMost = root;
while (leftMost != null) {
// start from leftMost node to traverse current level
// and connect the next level
Node cur = leftMost; // current node at current level
Node dummy = new Node(0); // used to store head of next level
Node prev = dummy; // the leading node of next level
// use dummy to avoid null check to simplify the implementation
while (cur != null) {
// traverse current level to connect next level node
// populate prev.next to next node in the next level
if (cur.left != null) {
prev.next = cur.left;
prev = prev.next;
}
if (cur.right != null) {
prev.next = cur.right;
prev = prev.next;
}
// move to next node in current level
cur = cur.next;
}
// move leftMost to left most node (head) of next level
leftMost = dummy.next;
}
return root;
}
private static class Node {
int val;
Node left;
Node right;
Node next;
public Node(int val) {
this.val = val;
}
}
}