forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulatingNextRightPointersInEachNode.java
46 lines (42 loc) · 1.33 KB
/
PopulatingNextRightPointersInEachNode.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
public class PopulatingNextRightPointersInEachNode {
/** 递归法,巧妙地运用dummy使代码很简洁
* 假定当前root所在层已连好,要连下一层
*/
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);
}
/**
* 将递归转成非递归很简单,就加一层循环,且结尾处加root = dummy.next即可
* @param root
*/
public void connect2(TreeLinkNode root) {
while (root != null) {
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;
}
}
root = dummy.next;
}
}
}