携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第19天,点击查看活动详情
题目描述
完全二叉树 是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。
设计一种算法,将一个新节点插入到一个完整的二叉树中,并在插入后保持其完整。
实现 CBTInserter 类:
CBTInserter(TreeNode root) 使用头节点为 root 的给定树初始化该数据结构; CBTInserter.insert(int v) 向树中插入一个值为 Node.val == val的新节点 TreeNode。使树保持完全二叉树的状态,并返回插入节点 TreeNode 的父节点的值; CBTInserter.get_root() 将返回树的头节点。
示例 1:
输入
["CBTInserter", "insert", "insert", "get_root"]
[[[1, 2]], [3], [4], []]
输出
[null, 1, 2, [1, 2, 3, 4]]
解释
CBTInserter cBTInserter = new CBTInserter([1, 2]);
cBTInserter.insert(3); // 返回 1
cBTInserter.insert(4); // 返回 2
cBTInserter.get_root(); // 返回 [1, 2, 3, 4]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/complete-binary-tree-inserter
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路分析
- 今天的的算法题目是设计类题目。题目描述比较长,我们跟着例子来理解。首先完全二叉树的定义已经给出了。是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。根据这个提示,我们可以按照层来考虑。
- 层序遍历,一般是 BFS,我们一般使用队列这种数据结构,每一层都从左到右依次添加节点。当插入新的节点 child 的时候,先在队列中找 parent 节点,判断 parent 的左右子节点情况。如果左右子节点有空的情况,则插入,如果左右子节点都满了,则出队。然后把 child 节点插入队列中。
- 具体实现代码如下,供参考。
通过代码
class CBTInserter {
Queue<TreeNode> candidate;
TreeNode root;
public CBTInserter(TreeNode root) {
this.candidate = new ArrayDeque<TreeNode>();
this.root = root;
Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
if (!(node.left != null && node.right != null)) {
candidate.offer(node);
}
}
}
public int insert(int val) {
TreeNode child = new TreeNode(val);
TreeNode node = candidate.peek();
int ret = node.val;
if (node.left == null) {
node.left = child;
} else {
node.right = child;
candidate.poll();
}
candidate.offer(child);
return ret;
}
public TreeNode get_root() {
return root;
}
}
总结
- 二叉树是我们最常见的数据结构,考察题型众多,解题思想相对固定,我们多加练习,就能掌握好!
- 坚持算法每日一题,加油!