-
Notifications
You must be signed in to change notification settings - Fork 21
/
Populating Next Right Pointers in Each Node.java
executable file
·147 lines (129 loc) · 4.25 KB
/
Populating Next Right Pointers in Each Node.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
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
M
1519715027
tags: Tree, DFS, Divide and Conquer
给一个特殊的binary tree, treeNode里面有一个 next pointer.
写一个function, 把所有node都更同level的node 连在一起. 最右边的node.next = NULL
#### DFS + Divide and Conquer
- 题目要求DFS. 想清楚了如何在DFS level把几种情况都考虑了, 写起来很简单. NOT BFS, because requires O(1) space
- 对于一个root来说, 只有几个点可以顾忌到: root.left, root.right, root.next.
- 想办法把这三个方向的点, 能连起来的都连起来:
- 1. `node.left.next = node.right`
- 2. If `node.next != null`, link `node.right.next = node.next.left`;
- 然后在dfs(root.left), dfs(root.right)
- Time: visit && connect all nodes, O(n)
#### BFS
- 不和题意,用了queue space,与Input成正比。太大。
- BFS over Tree。 用Queue 和 queue.size(),老规矩。
- process每层queue时, 注意把next pointer加上去就好.
```
/*
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *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.
Note:
You may only use constant extra space.
You may assume that it is a perfect binary tree
(ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
Hide Tags Tree Depth-first Search
Hide Similar Problems (H) Populating Next Right Pointers in Each Node II (M) Binary Tree Right Side View
*/
/**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
/*
Thoughts:
BFS with queue will be trival, but 2^(logN) = N, so O(n) space can't do.
DFS: at each level, carefully link:
1. node.left.next = node.right
2. If node.next != null, link node.right.next = node.next.left;
*/
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null || root.left == null || root.right == null) {
return;
}
root.left.next = root.right;
if (root.next != null) {
root.right.next = root.next.left;
}
connect(root.left);
connect(root.right);
}
}
// Previous solution. Actuall no need of a helper dfs.
//DFS. Basic implementation according to problem.
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null || (root.left == null && root.right == null)) {
return;
}
root.left.next = root.right;
dfs(root.left);
dfs(root.right);
}
public void dfs(TreeLinkNode node) {
if (node == null || node.left == null || node.right == null) {
return;
}
node.left.next = node.right;
if (node.next != null)
node.right.next = node.next.left;
dfs(node.left);
dfs(node.right);
}
}
// # of leaf nodes: 2 ^ (height) ~= 2 ^ (logN) = N => used O(N) space
//BFS, However, 不和题意。 point each node to the next in queue. if none, add null
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null || (root.left == null && root.right == null)) {
return;
}
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.offer(root);
int size = 0;
while (!queue.isEmpty()) {
size = queue.size();
for (int i = 0; i < size; i++) {
TreeLinkNode node = queue.poll();
if (i < size - 1) {
node.next = queue.peek();
} else {
node.next = null;
}
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
size = queue.size();
}
}
}
```