当前位置:网站首页>Force buckle 919. Complete binary tree inserter
Force buckle 919. Complete binary tree inserter
2022-07-27 21:15:00 【Ruthless young Fisherman】
subject
Perfect binary tree Every floor ( Except for the last floor ) All completely filled ( namely , The number of nodes reaches the maximum ) Of , And all nodes are concentrated on the left as much as possible .
Design an algorithm , Insert a new node into a complete binary tree , And keep it intact after insertion .
Realization CBTInserter class :
CBTInserter(TreeNode root) Use the header node as root Initialize the data structure for the given tree ;
CBTInserter.insert(int v) Insert a value of... Into the tree Node.val == val The new node TreeNode. Keep the tree in the state of a complete binary tree , And return to the insert node TreeNode The value of the parent node of ;
CBTInserter.get_root() The head node of the tree will be returned .
Example

Input
[“CBTInserter”, “insert”, “insert”, “get_root”]
[[[1, 2]], [3], [4], []]
Output
[null, 1, 2, [1, 2, 3, 4]]
explain
CBTInserter cBTInserter = new CBTInserter([1, 2]);
cBTInserter.insert(3); // return 1
cBTInserter.insert(4); // return 2
cBTInserter.get_root(); // return [1, 2, 3, 4]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/complete-binary-tree-inserter
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: Queue simulation 
Java Realization
class CBTInserter {
Queue<TreeNode> queue, candidate;
TreeNode root;
public CBTInserter(TreeNode root) {
this.root = root;
queue = new LinkedList<>();
candidate = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int sz = queue.size();
for (int i = 0; i < sz; i++) {
TreeNode cur = queue.poll();
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
// If the left subtree is empty or the right subtree is empty, add candidate queue
if (!(cur.left != null && cur.right != null)) candidate.offer(cur);
}
}
}
public int insert(int val) {
TreeNode node = candidate.peek();
TreeNode var = new TreeNode(val);
if (node.left == null) {
node.left = var;
} else {
// here , The right subtree also inserts nodes . The left and right subtrees of this node are not empty
// therefore , From you to candidate Pop up this node in .
node.right = var;
candidate.poll();
}
candidate.offer(var);
return node.val;
}
public TreeNode get_root() {
return root;
}
}

边栏推荐
- MapGIS三维管线建模,唤醒城市地下管线脉搏
- Diffuse reflection of QT OpenGL light
- 飞信卒于2022:中国移动一手好牌被打烂,5亿用户成“僵尸”
- 飞桨框架体验评测交流会,产品的使用体验由你来决定!
- Automated testing - unittest framework
- Brand list cases
- 原生对象、内置对象、宿主对象的区别
- 数字引领 规划先行 聚焦智慧规划信息平台建设及应用项目探索实践
- Summary of common methods and attributes of arrays and strings in JS
- Knowledge management system promotes the development of enterprise informatization
猜你喜欢
![论文赏析[AAAI18]面向序列建模的元多任务学习](/img/2b/345b5a287fcd9c9b1a86ae683f124b.png)
论文赏析[AAAI18]面向序列建模的元多任务学习

Dobot Magician 机器臂-简介

成分句法分析综述(第二版)

Conquer 3 pieces of IT equipment for all programmers →

MapGIS三维管线建模,唤醒城市地下管线脉搏

Lidar China's front loading curtain opens, millions of production capacity to be digested

如何让个性化推荐即刻触达?云原生数据库GaussDB(for Redis)来助力

自动化测试----unittest框架
![论文赏析[EMNLP18]用序列标注来进行成分句法分析](/img/99/98f3e5795407c764907e957f69df10.png)
论文赏析[EMNLP18]用序列标注来进行成分句法分析

JS closure knowledge
随机推荐
常见ArrayLIst面试题
自定义学习率
One of IOU target tracking: IOU tracker
Understanding network model TCPIP model
中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设
如何查看蓝牙耳机的蓝牙版本
Automated testing - unittest framework
Knife4j dynamically refreshes global parameters through JS
Recommend a powerful search tool listary
IOU target tracking II: viou tracker
飞桨框架体验评测交流会,产品的使用体验由你来决定!
飞信卒于2022:中国移动一手好牌被打烂,5亿用户成“僵尸”
【历史上的今天】7 月 27 日:模型检测先驱出生;微软收购 QDOS;第一张激光照排的中文报纸
redis cook book.notes.
Summary of common methods and attributes of arrays and strings in JS
LeetCode每日一练 —— 203. 移除链表元素
一种比读写锁更快的锁,还不赶紧认识一下
Sre related question answering
Qt opengl 让物体在关照下动起来,形成动画
Natapp intranet penetration tool Internet access personal projects