Topic
- Tree
- Depth-first Search
- Breadth-first Search
Description
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
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:
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:
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
Analysis
方法一:DFS,但递归会用到隐式非常量额外空间,不符题意。
方法二:BFS,但用到非常量额外空间的队列,不符题意。
方法三:子节点们利用父节点的next进行连接。
Submission
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class PopulatingNextRightPointersInEachNode {
public static 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;
}
};
// 方法一:DFS
public Node connect(Node root) {
if (root == null)
return root;
connect(root, 0, new ArrayList<>());
return root;
}
private void connect(Node node, int level, List<Node> prevs) {
if (node == null)
return;
if (prevs.size() == level) {
prevs.add(node);
} else {
Node prev = prevs.get(level);
prev.next = node;
prevs.set(level, node);
}
connect(node.left, level + 1, prevs);
connect(node.right, level + 1, prevs);
}
// 方法二:BFS
public Node connect2(Node root) {
if (root == null)
return root;
LinkedList<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node prev = null;
for (int size = queue.size(); size > 0; size--) {
Node node = queue.poll();
if (prev != null) {
prev.next = node;
}
prev = node;
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
}
}
return root;
}
// 方法三:利用父节点的next
public Node connect3(Node root) {
if (root == null)
return null;
Node pre = root;
Node cur = null;
while (pre.left != null) {
cur = pre;
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;
}
pre = pre.left;
}
return root;
}
}
Test
public class PopulatingNextRightPointersInEachNodeTest {
@Test
public void test() {
PopulatingNextRightPointersInEachNode obj = new PopulatingNextRightPointersInEachNode();
Node root = n(1, n(2, n(4), n(5)), n(3, n(6), n(7)));
root = obj.connect(root);
assertEquals("1,#,2,3,#,4,5,6,7,#", output(root));
}
@Test
public void test2() {
PopulatingNextRightPointersInEachNode obj = new PopulatingNextRightPointersInEachNode();
Node root = n(1, n(2, n(4), n(5)), n(3, n(6), n(7)));
root = obj.connect2(root);
assertEquals("1,#,2,3,#,4,5,6,7,#", output(root));
}
@Test
public void test3() {
PopulatingNextRightPointersInEachNode obj = new PopulatingNextRightPointersInEachNode();
Node root = n(1, n(2, n(4), n(5)), n(3, n(6), n(7)));
root = obj.connect3(root);
assertEquals("1,#,2,3,#,4,5,6,7,#", output(root));
}
private Node n(int v) {
return new Node(v);
}
private Node n(int v, Node l, Node r) {
return new Node(v , l, r, null);
}
private String output(Node root) {
if(root == null)
return "";
StringBuilder sb = new StringBuilder();
Node p = root;
while(p != null) {
Node p2 = p;
sb.append(p2.val);
sb.append(',');
while(p2.next != null) {
p2 = p2.next;
sb.append(p2.val);
sb.append(',');
}
sb.append("#,");
p = p.left;
}
return sb.substring(0, sb.length() - 1);
}
}