forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulatingNextRightPointersInEachNodeII.java
42 lines (39 loc) · 1.26 KB
/
PopulatingNextRightPointersInEachNodeII.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
public class PopulatingNextRightPointersInEachNodeII {
/**
* 和上题的区别在于这里root下一个循环要指向dummy.next,即下一层链表表头
*/
public void connect(TreeLinkNode root) {
while (root != null) {
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
for (TreeLinkNode node = root; node != null; node = node.next) {
if (node.left != null) {
cur.next = node.left;
cur = cur.next;
}
if (node.right != null) {
cur.next = node.right;
cur = cur.next;
}
}
root = dummy.next;
}
}
/** 递归,和上题相比改都不用改
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
for (TreeLinkNode p = root; p != null; p = p.next) {
if (p.left != null) {
cur.next = p.left;
cur = cur.next;
}
if (p.right != null) {
cur.next = p.right;
cur = cur.next;
}
}
connect(dummy.next);
}*/
}