-
Tree
-
Depth-first Search
-
Breadth-first Search
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
方法一:DFS,但递归会用到隐式非常量额外空间,不符题意。
方法二:BFS,但用到非常量额外空间的队列,不符题意。
方法三:子节点们利用父节点的next进行连接。
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 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 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;
}
}
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);
Vue
-
什么是MVVM?
-
mvvm和mvc区别?它和其它框架(jquery)的区别是什么?哪些场景适合?
-
组件之间的传值?
-
Vue 双向绑定原理
-
描述下 vue 从初始化页面–修改数据–刷新页面 UI 的过程?
-
虚拟 DOM 实现原理
-
Vue 中 key 值的作用?
-
Vue 的生命周期
-
Vue 组件间通信有哪些方式?
-
vue 中怎么重置 data?
-
组件中写 name 选项有什么作用?
-
Vue 的 nextTick 的原理是什么?
-
Vuex 有哪几种属性?