当前位置:网站首页>Class, leetcode919 -- complete binary tree inserter
Class, leetcode919 -- complete binary tree inserter
2022-07-28 23:42:00 【dor.yang】
Topic content
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 1:

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]
Tips :
The number of nodes in the tree ranges from [1, 1000]
0 <= Node.val <= 5000
root It's a perfect binary tree
0 <= val <= 5000
Each test case calls at most insert and get_root operation 104 Time
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 .
Their thinking
In fact, if you say the meaning of this topic , In fact, it's easy to understand , Is a complete binary tree + The problem of nodes , But one of the trouble is the construction of binary tree , They provided a binary tree , But when I first understood the function of initialization function, there was a deviation , I think we should build a complete binary tree by ourselves , I tried to achieve this , The effect is not good .
After reading the idea of the problem solution, I realized that the function of the initialization function actually has no clear restrictions , It's just for the later functions . As long as all leaf nodes and a possible node with only left node but no right node are stored in the queue during initialization . Then go to an out connection, which is the insertion of binary tree . As for returning to the parent node , The process of finding the node is output .
This is the first question that I do this kind of multiple function types on the force buckle , I still feel a little unnatural , I hope it will be better after more practice , Just like yesterday's weekly race , Encounter the problem of writing classes by multiple functions , I can't start because of lack of experience , So I still have to step up my practice .
Implementation code
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class CBTInserter {
private:
TreeNode* node;
queue<TreeNode*> link;
public:
CBTInserter(TreeNode* root) {
node=root;
queue<TreeNode*> q;
if(!root){
node=nullptr;
}
q.push(root);
while(!q.empty()){
TreeNode* dian=q.front();
q.pop();
if(dian->left){
q.push(dian->left);
}
if(dian->right){
q.push(dian->right);
}
if(!dian->left||!dian->right){
link.push(dian);
}
}
}
int insert(int val) {
TreeNode* dian=new TreeNode(val);
TreeNode* mid=link.front();
if(mid->left){
mid->right=dian;
link.pop();
link.push(dian);
}
else{
mid->left=dian;
link.push(dian);
}
return mid->val;
}
TreeNode* get_root() {
return node;
}
};
/** * Your CBTInserter object will be instantiated and called as such: * CBTInserter* obj = new CBTInserter(root); * int param_1 = obj->insert(val); * TreeNode* param_2 = obj->get_root(); */
边栏推荐
- 经典双栈实现队列,注意遍历栈的判定条件修改。
- 2022 welder (Junior) work license questions and answers
- Byte 8 years' experience of female test Director - for girls who want to change careers or are about to enter the testing industry
- MySQL functions
- How strong is this glue?
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- 解决线程安全问题&&单例模式
- 刨根问底学 二叉树
- 1314_串口技术_RS232通信基础的信息
- 「行泊一体」放量,福瑞泰克高性能域控制器领跑新赛道
猜你喜欢

xss.haozi.me靶场详解

2022t elevator repair examination questions and simulation examination

Wechat applet development ④

Rhce第一天

浪潮ClusterEngineV4.0 远程命令执行漏洞 CVE-2020-21224

2022 R2 mobile pressure vessel filling test question simulation test platform operation

Arduino uno driver universe 1.8 'TFT SPI screen example demonstration (including data package)
![[self] - brush questions BFS](/img/e9/e90557c63c217a43c6a5d9de0d0869.png)
[self] - brush questions BFS

KingbaseES客户端编程接口指南-ODBC(4. 创建数据源)

Go 中的并发 Concurrency
随机推荐
22牛客多校day1 J - Serval and Essay 启发式合并
Manufacturing steps of interactive slide screen in exhibition hall
集火全屋智能“后装市场”,真正玩得转的没几个
mongodb索引添加、查看、导出、删除
Achieve high throughput through Wi Fi 7 - insight into the next generation of Wi Fi physical layer
Huawei wireless device configuration uses WDS technology to deploy WLAN services
金仓数据库 KingbaseES V8.3至V8.6迁移最佳实践(2. KingbaseES V8.3和 V8.6 兼容性)
2022g3 boiler water treatment test simulation 100 questions simulation test platform operation
KingbaseES客户端编程接口指南-ODBC(4. 创建数据源)
How to add the index of a set in mongodb to another set in mongodb
Intel data center GPU is officially shipped to provide strong computing power with openness and flexibility
Shenkaihong: on the river of Zhilian of all things, there is a bright moon of open source
MySQL functions
Codeforces Round #474 (Div. 1 + Div. 2) - C, F
Asynchronism and synchronization of visa write and read functions by LabVIEW
wget什么意思
Input element label
[self] - brush questions array
In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
2022 simulated examination platform operation of hoisting machinery command examination questions