当前位置:网站首页>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(); */
边栏推荐
- 金仓数据库KingbaseES 客户端编程接口指南 - ODBC特性支持约束
- Routeros limited DNS hijacking and check
- 【数据挖掘工程师-笔试】2022年大华股份
- LabVIEW对VISA Write和Read函数的异步和同步
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
- Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?
- How to embed AI digital human function in VR panorama? Create a cloud experience
- 机器学习问题笔记
- JSP tag case
- MySQL log management, backup and recovery
猜你喜欢

Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)

深度剖析集成学习GBDT

深度剖析集成学习Adaboost

互动滑轨屏在展厅中应用的制作步骤

MySQL introduction

深度剖析集成学习Xgboost
Object object

trivy【3】自定义扫描策略

苹果官网正在更新维护 Apple Store,国行 iPhone 13 / Pro 等产品将最高优惠 600 元

How to embed AI digital human function in VR panorama? Create a cloud experience
随机推荐
Object object
Merkle tree
Huawei wireless device configuration uses WDS technology to deploy WLAN services
金仓数据库KingbaseES客户端编程接口指南-ODBC(5. 开发过程)
What if win11 cannot find the DNS address? Win11 can't find DNS and can't access the web page solution
【自】-刷题-BFS
苹果官网正在更新维护 Apple Store,国行 iPhone 13 / Pro 等产品将最高优惠 600 元
Array array object
2022起重机械指挥考试题模拟考试平台操作
网络流量监控工具iftop
Wechat applet development ③
2022年R2移动式压力容器充装考题模拟考试平台操作
JS small method
2022年G2电站锅炉司炉考试题库模拟考试平台操作
xss.haozi.me靶场详解
ACM SIGIR 2022 | 美团技术团队精选论文解读
如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
Messages from students participating in the competition: memories of the 17th session
电脑不知卸载什么,打不开计算器无法编辑截图功能打不开txt文件等等解决方案之一
【自】-刷题-动态规划