0%

Leetcode116-populatingNextRightPointersInEachNode

Description

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

1
2
3
4
5
6
struct Node {
int val;
Node *left;
Node *right;
Node *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.

Follow up:

  • You may only use constant extra space.
  • Recursive approach is fine, you may assume implicit stack space does not count as extra space for this problem.

Example


1
2
3
Input: root = [1,2,3,4,5,6,7]
Output: [1,#,2,3,#,4,5,6,7,#]
Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.

Constraints:

  • The number of nodes in the given tree is less than 4096.
  • -1000 <= node.val <= 1000

Solution

Solution 1: Basic solution

Just used two pointers, one to record each level’s heads, one to traversal nodes in one level.

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
/*
// 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;

Node head = root;
while(head != null){
Node cur = head;
while(cur != null){
if (cur.left != null) cur.left.next = cur.right;
if (cur.right != null && cur.next != null) cur.right.next = cur.next.left;
cur = cur.next;
}
head = head.left;
}
return root;
}
}

Solution 2: General Solution

As the solution in next probelm. Used three pointers.

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
/*
// 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;

Node pre = null;
Node head = null;
Node cur = root;
while (cur != null){

while(cur != null){
if (cur.left != null){
if (head == null)
head = cur.left;
if (pre != null)
pre.next = cur.left;
pre = cur.left;
}
if (cur.right != null){
if (head == null)
head = cur.right;
if (pre != null)
pre.next = cur.right;
pre = cur.right;
}
cur = cur.next;
}

cur = head;
head = null;
pre = null;
}

return root;
}
}