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

边栏推荐
- 14 day Hongmeng device development practice - Chapter 7 device networking cloud learning notes
- The use experience of the product is up to you in the evaluation and exchange meeting of the flying oar frame experience!
- Obtain website shell permission based on file upload vulnerability
- 电脑微软账户登录一直转圈怎么解决问题
- Understand the communication mode of transmission media
- Automated testing ----- selenium (II)
- 飞桨框架体验评测交流会,产品的使用体验由你来决定!
- Diffuse reflection of QT OpenGL light
- What are the application scenarios of real name authentication in the cultural tourism industry?
- LabVIEW learning note 9: capture the "value change" event generated by the program modifying the control value
猜你喜欢

Good driving, inexpensive, comfortable and safe! Experience BYD song Pro DM-I in depth

PHP代码审计6—文件包含漏洞

一种比读写锁更快的锁,还不赶紧认识一下

LeetCode每日一练 —— 链表中倒数第 k 个结点

How to solve the problem when the Microsoft account login of the computer keeps turning around
![论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles](/img/bf/5244eafd927ae990551a59dfa7e863.png)
论文赏析[EMNLP18]针对自顶向下和中序移进归约成分句法分析的Dynamic Oracles

JS closure knowledge

How to check the Bluetooth version of Bluetooth headset

LeetCode-209-长度最小的子数组

82. (cesium article) cesium points move on 3D models
随机推荐
Remember that resttemplate.getforentity failed to carry headers once, resttemplate exchange
Summary of common methods and attributes of arrays and strings in JS
[today in history] July 27: model testing pioneer was born; Microsoft acquires qdos; The first laser typesetting Chinese newspaper
Win11 widget prompts how to solve the error when loading this content?
自动化测试----selenium(二)
Repeated DNA sequence [hash determination repetition + sliding window + bit operation of binary coding]
Hexagon_ V65_ Programmers_ Reference_ Manual(5)
Set up discuz forum and break the stolen database
redis cook book.notes.
Overview of understanding the physical layer of transmission media
Automated testing - unittest framework
LeetCode每日一练 —— 206. 反转链表
常见ArrayLIst面试题
【历史上的今天】7 月 27 日:模型检测先驱出生;微软收购 QDOS;第一张激光照排的中文报纸
Win11用户名和密码备份方法
Sscanf caused the address to be out of bounds
Tips for file upload to bypass WAF
LabVIEW learning note 5: you cannot return to the original state after pressing the button
NATAPP内网穿透工具外网访问个人项目
Opencv implements image clipping and scaling