-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117-popuolateNextTree2.java
More file actions
43 lines (38 loc) · 1.04 KB
/
117-popuolateNextTree2.java
File metadata and controls
43 lines (38 loc) · 1.04 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
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val,Node _left,Node _right,Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
// 普通二叉树,找一层的最右边,从上到下,从右到左思考
class Solution_117 {
public Node connect(Node root) {
if(root == null) return null;
if(root.left!=null){
if(root.right!=null){
root.left.next = root.right;
} else {
root.left.next = nextNode(root.next);
}
}
if(root.right != null) {
root.right.next = nextNode(root.next);
}
connect(root.right);
connect(root.left);
return root;
}
public Node nextNode(Node node) {
if(node==null) return null;
if(node.left!=null) return node.left;
if(node.right!=null) return node.right;
return nextNode(node.next);
}
}