当前位置:网站首页>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;
}
}

边栏推荐
- Qt 链接MSSQL
- Sre related question answering
- How to solve the problem that tp6 controller does not exist: app\controller\index
- Uncaught SyntaxError: redeclaration of let page
- Elk too heavy? Try KFC log collection
- 自定义学习率
- PHP code audit 5 - XSS vulnerability
- Leetcode daily practice - 876. Intermediate node of linked list
- LeetCode-136-只出现一次的数字
- 飞信卒于2022:中国移动一手好牌被打烂,5亿用户成“僵尸”
猜你喜欢
![Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]](/img/ed/6f4da22e86b44935fc84e3b4901c48.png)
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]

LeetCode每日一练 —— 21. 合并两个有序链表

LeetCode每日一练 —— 876. 链表的中间结点

MapGIS三维场景渲染技术与应用

Leetcode daily practice 206. Reverse the linked list

CPDA | how to have data analysis thinking?

IOU target tracking II: viou tracker

82. (cesium article) cesium points move on 3D models

中地数码:融合创新国产GIS 乘风而上助推实景三维中国建设

LabVIEW learning note 5: you cannot return to the original state after pressing the button
随机推荐
Automated testing - unittest framework
Unity 安装个人免费版
力扣 919. 完全二叉树插入器
R language uses LROC function of epidisplay package to visualize ROC curve of logistic regression model and output diagnostic table, visualize multiple ROC curves, and use legend function to add legen
文件上传绕过WAF的技巧大全
用伪元素before实现元素的等比例缩放
Win11系统更新KB5014668后点开始按钮没反应怎么办?
PHP代码审计6—文件包含漏洞
Digital leading planning first, focusing on the construction of intelligent planning information platform and the exploration and practice of application projects
Second uncle, why is it so hot?
[R language] [1] beginners learn the grammar of R language and edit it with rstudio
Differences among native objects, built-in objects, and host objects
Zhongdi Digital: integrating innovative domestic GIS to boost the construction of real 3D China
NATAPP内网穿透工具外网访问个人项目
Automated testing ----- selenium (II)
R language uses dplyr package to perform data aggregation statistics, calculate sliding window statistics, calculate sliding group mean, and merge the generated statistical data into the original data
“地理-语言”大模型文心ERNIE-GeoL及应用
一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由
如何查看蓝牙耳机的蓝牙版本
Leetcode daily practice - cm11 linked list segmentation