LC.P117[填充每个节点的下一个右侧节点指针II]

方法一:BFS

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
/*
// 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 null;
Deque<Node> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Node> list = new ArrayList<>();
while (size-- > 0) {
Node cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
list.add(cur);
}
for (int i = 0; i < list.size() - 1; ++i) {
list.get(i).next = list.get(i + 1);
}
}
return root;
}
}

优化

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
/*
// 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 null;
Deque<Node> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
Node p = null;
while (size-- > 0) {
Node cur = queue.poll();
if (p != null) {
p.next = cur;
}
p = cur;
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
return root;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$

方法二:DFS

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
/*
// 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 {
List<Node> pre = new ArrayList<>();

public Node connect(Node root) {
dfs(root, 0);
return root;
}

private void dfs(Node node, int depth) {
if (node == null) return;
if (depth == pre.size()) {
// node 是这一层最左边的节点
pre.add(node);
} else {
pre.get(depth).next = node;
pre.set(depth, node);
}
dfs(node.left, depth + 1);
dfs(node.right, depth + 1);
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(h)$,$h$为树的高度

方法三:(优化)BFS+链表

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
/*
// 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) {
Node dummy = new Node(), cur = root;
while (cur != null) {
dummy.next = null;
Node next = dummy; // 下一层的链表
while (cur != null) { // 遍历当前层的链表
if (cur.left != null) { // 将下一层的相邻节点连起来
next.next = cur.left;
next = cur.left;
}
if (cur.right != null) { // 将下一层的相邻节点连起来
next.next = cur.right;
next = cur.right;
}
cur = cur.next; // 当前层链表的下一个节点
}
cur = dummy.next; // 下一层链表的头节点
}
return root;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$