-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117.populating-next-right-pointers-in-each-node-ii.java
More file actions
165 lines (158 loc) · 6.26 KB
/
Copy path117.populating-next-right-pointers-in-each-node-ii.java
File metadata and controls
165 lines (158 loc) · 6.26 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* @lc app=leetcode id=117 lang=java
*
* [117] Populating Next Right Pointers in Each Node II
*/
// @lc code=start
/*
// Definition for a Node.
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;
}
};
*/
class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
Queue<Node> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while (!nodeQueue.isEmpty()) {
int currLevelCnt = nodeQueue.size();
Node prevNode = null;
for (int i = 0; i < currLevelCnt; i++) {
Node currNode = nodeQueue.poll();
if (prevNode != null) {
prevNode.next = currNode;
}
if (currNode.left != null) {
nodeQueue.add(currNode.left);
}
if (currNode.right != null) {
nodeQueue.add(currNode.right);
}
prevNode = currNode;
}
}
return root;
}
// 这里由于子树有可能残缺,故需要平行扫描父节点同层的节点,找到他们的左右子节点
// recursive 方法
// public Node connect(Node root) {
// if (root == null) {
// return root;
// }
// if (root.left != null) {
// if (root.right != null) {
// root.left.next = root.right;
// }
// else {
// Node nextHasChildNode = root.next;
// if (nextHasChildNode != null) {
// while (nextHasChildNode.next != null && nextHasChildNode.left == null && nextHasChildNode.right == null) {
// nextHasChildNode = nextHasChildNode.next;
// }
// if (nextHasChildNode.left != null) {
// root.left.next = nextHasChildNode.left;
// }
// else if (nextHasChildNode.right != null) {
// root.left.next = nextHasChildNode.right;
// }
// }
// }
// }
// if (root.right != null) {
// Node nextHasChildNode = root.next;
// if (nextHasChildNode != null) {
// while (nextHasChildNode.next != null && nextHasChildNode.left == null && nextHasChildNode.right == null) {
// nextHasChildNode = nextHasChildNode.next;
// }
// if (nextHasChildNode.left != null) {
// root.right.next = nextHasChildNode.left;
// }
// else if (nextHasChildNode.right != null) {
// root.right.next = nextHasChildNode.right;
// }
// }
// }
// connect(root.right);
// connect(root.left);
// return root;
// }
// // 这里由于子树有可能残缺,故需要平行扫描父节点同层的节点,找到他们的左右子节点
// // constant space O(1),runtime 接近O(N)
// public Node connect(Node root) {
// if (root == null) {
// return root;
// }
// Node currNode = null, startNode = root;
// while (startNode != null) {
// currNode = startNode;
// while (currNode != null) {
// if (currNode.left != null) {
// if (currNode.right != null) {
// currNode.left.next = currNode.right;
// }
// else {
// // 首先找到下一个有子节点的Node
// // 再把它的子节点和当前node的左子节点连起来
// Node nextHasChildNode = currNode.next;
// if (nextHasChildNode != null) {
// while (nextHasChildNode.next != null && nextHasChildNode.left == null && nextHasChildNode.right == null) {
// nextHasChildNode = nextHasChildNode.next;
// }
// if (nextHasChildNode.left != null) {
// currNode.left.next = nextHasChildNode.left;
// }
// else if (nextHasChildNode.right != null) {
// currNode.left.next = nextHasChildNode.right;
// }
// }
// }
// }
// if (currNode.right != null) {
// // 首先找到下一个有子节点的Node
// // 再把它的子节点和当前node的右子节点连起来
// Node nextHasChildNode = currNode.next;
// if (nextHasChildNode != null) {
// while (nextHasChildNode.next != null && nextHasChildNode.left == null && nextHasChildNode.right == null) {
// nextHasChildNode = nextHasChildNode.next;
// }
// if (nextHasChildNode.left != null) {
// currNode.right.next = nextHasChildNode.left;
// }
// else if (nextHasChildNode.right != null) {
// currNode.right.next = nextHasChildNode.right;
// }
// }
// }
// currNode = currNode.next;
// }
// // 找到下一个startNode,必须是有子节点并且和当前startNode在同一层的node
// while (startNode.next != null && startNode.left == null && startNode.right == null) {
// startNode = startNode.next;
// }
// if (startNode.left != null) {
// startNode = startNode.left;
// }
// else {
// startNode = startNode.right;
// }
// }
// return root;
// }
}
// @lc code=end