-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopulatingNextRightPointersInEachNode.java
More file actions
85 lines (77 loc) · 2.28 KB
/
PopulatingNextRightPointersInEachNode.java
File metadata and controls
85 lines (77 loc) · 2.28 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package binary_tree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* @Author: Wenhang Chen
* @Description:给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下: struct Node {
* int val;
* Node *left;
* Node *right;
* Node *next;
* }
* 填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
* <p>
* 初始状态下,所有 next 指针都被设置为 NULL。
* @Date: Created in 11:27 1/30/2020
* @Modified by:
*/
public class PopulatingNextRightPointersInEachNode {
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
}
public Node connect(Node root) {
Queue<Node> queue = new LinkedList<>();
if (root != null) queue.add(root);
int curCount = 1;
int nextCount = 0;
while (!queue.isEmpty()) {
curCount--;
Node node = queue.poll();
if (node.left != null) {
queue.add(node.left);
nextCount++;
}
if (node.right != null) {
queue.add(node.right);
nextCount++;
}
if (curCount == 0) {
curCount = nextCount;
nextCount = 0;
node.next = null;
} else {
node.next = queue.peek();
}
}
return root;
}
// 递归解法,先劈成两半处理之间的,再逐一对两子树进行处理
public Node connect2(Node root) {
if (root == null) return root;
Node left = root.left;
Node right = root.right;
while (left != null) {
left.next = right;
left = left.right;
right = right.left;
}
connect2(root.left);
connect2(root.right);
return root;
}
}