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

边栏推荐
- 基于文件上传漏洞获得网站 shell 权限
- LeetCode-209-长度最小的子数组
- "Harvest" NFT: 200 yuan to buy pictures on Taobao, and 300000 yuan on the chain
- MAPGIS 3D pipeline modeling awakens the pulse of urban underground pipelines
- Hexagon_ V65_ Programmers_ Reference_ Manual(9)
- Chapter 7 Intermediate Shell Tool I
- 二舅,为什么火了?
- PHP code audit 6 - file contains vulnerability
- Introduction to source insight 4.0
- How to talk to CIO / CTO
猜你喜欢

LeetCode每日一练 —— 206. 反转链表

One article to understand pychar shortcut key

PG free space map & visibility map

Rust变量特点

Leetcode daily practice - the penultimate node in the linked list

PHP code audit 6 - file contains vulnerability
![论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles](/img/bf/5244eafd927ae990551a59dfa7e863.png)
论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles

一文读懂Plato&nbsp;Farm的ePLATO,以及其高溢价缘由

Uncaught SyntaxError: redeclaration of let page

力扣 919. 完全二叉树插入器
随机推荐
How to make personalized recommendations instantly accessible? Cloud native database gaussdb (for redis) to help
Opencv implements image clipping and scaling
PG free space map & visibility map
基于文件上传漏洞获得网站 shell 权限
Uncaught SyntaxError: redeclaration of let page
PHP代码审计6—文件包含漏洞
建筑云渲染的应用正在扩大,越来越多的行业急需可视化服务
论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles
Win11系统更新KB5014668后点开始按钮没反应怎么办?
Read Plato & nbsp; Eplato of farm and the reasons for its high premium
数字化工厂系统有什么现实优势
Codeforces 1706e merge + heuristic merge + st table
知识管理系统推动企业信息化发展
Knowledge management system promotes the development of enterprise informatization
R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses deviance function to calculate the sum of squares of residual
Custom learning rate
CPDA | how to have data analysis thinking?
Understanding network model overview of network model
访问共享文件夹时提示“因为文件共享不安全 SMB1协议”请使用SMB2协议
Qt OPenGL 光的漫反射