题目:
完全二叉树是每一层(除最后一层外)都是完全填充
(即,节点数达到最大,第 n 层有 2n-1 个节点)的,并且所有的节点都尽可能地集中在左侧。
设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作:
CBTInserter(TreeNode root) 使用根节点为 root 的给定树初始化该数据结构;
CBTInserter.insert(int v) 向树中插入一个新节点,节点类型为 TreeNode,值为 v 。
使树保持完全二叉树的状态,并返回插入的新节点的父节点的值;
CBTInserter.get_root() 将返回树的根节点。
--------------
示例 1:
输入:inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]]
输出:[null,1,[1,2]]
示例 2:
输入:inputs = ["CBTInserter","insert","insert","get_root"],
inputs = [[[1,2,3,4,5,6]],[7],[8],[]]
输出:[null,3,4,[1,2,3,4,5,6,7,8]]
提示:
最初给定的树是完全二叉树,且包含 1 到 1000 个节点。
每个测试用例最多调用 CBTInserter.insert 操作 10000 次。
给定节点或插入节点的每个值都在 0 到 5000 之间。
------------------
思路:
首先,需要熟悉完全二叉树的特性:
完全二叉树定义
完全二叉树(Complete Binary Tree)若设二叉树的深度为h,除第 h 层外,
其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。
特性1:对于任意一个结点,不可能只存在右节点不存在左节点;
特性2:使用层序遍历法遍历整颗树,找到第一个没有两个子节点的节点,
其左侧的所有节点均有两个节点,其右侧的节点,均为叶子节点;
特性3:向一棵完全二叉树中插入节点,插入节点的位置均为先左后右;
使用上面的特性,在初始化时遍历整棵树放入队列中,
队列最终需要保存的节点为仍可能插入左节点或右节点的节点,
对于孩子已满的节点,从队列中弹出,不再需要。
--------------
class CBTInserter {
TreeNode root;
LinkedList<TreeNode> queue
public CBTInserter(TreeNode root) {
this.root = root;
queue = new LinkedList<TreeNode>();
}
//如果当前结点的左结点为空或者右结点为空,就将新结点接上,并将当前结点加回队头,并返回当前结点的值
//否则将当前结点的左右结点依次入队列,继续while循环
public int insert(int v) {
TreeNode newNode = new TreeNode(v);
if(queue.isEmpty()) queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(node.left == null){
node.left = newNode;
queue.addFirst(node);
}else if(node.right == null){
node.right = newNode;
queue.addFirst(node);
}else{
queue.add(node.left);
queue.add(node.right);
continue;
}
return node.val;
}
return -1;
}
public TreeNode get_root() {
return root;
}
}
一刷233-剑指 Offer II 043. 往完全二叉树添加节点(同:919. 完全二叉树插入器)
于 2022-03-27 14:43:11 首次发布