当前位置:网站首页>919. Complete binary tree inserter
919. Complete binary tree inserter
2022-07-25 19:30:00 【anieoo】
Original link :919. Complete binary tree inserter
solution:
/**
* 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 {
public:
TreeNode *R;
vector<TreeNode *> h;
CBTInserter(TreeNode* root) { // Establish a complete binary tree
h.resize(1);
R = root;
queue<TreeNode*> q;
q.push(root);
while (q.size()) {
auto t = q.front();
q.pop();
h.push_back(t);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
int insert(int val) {
auto t = new TreeNode(val);
h.push_back(t);
int k = h.size() - 1; // Location of nodes in the heap
int p = k / 2;
if(k == 2 * p) h[p]->left = t;
else h[p]->right = t;
return h[p]->val;
}
TreeNode* get_root() {
return R;
}
};
/**
* 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();
*/边栏推荐
- Pymoo learning (8):grades
- Alibaba cloud free SSL certificate application detailed process
- [record of question brushing] 21. Merge two ordered linked lists
- iMeta | Sangerbox: 交互式整合临床生信分析平台
- How to be a self disciplined person?
- Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
- Actual combat of MySQL database design project of online mall system
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
- Dynamic implementation of wechat applet 27 progress bar and static construction of search box and hot search list
- [wp]ctfshow-web入门信息搜集
猜你喜欢

Wechat campus maintenance application applet graduation design finished product of applet completion work (3) background function

解决Win10账户没有了管理员权限

网络数据包多层传输演示

小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板

微信小程序10-微搭模板

What is the application value of MES management system

Improvement of wechat applet 26 playing music page ②
![[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection](/img/22/426bcb8641db5bfe07e8aacf5e8427.png)
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection

binarySearch基础二分查找

基于PHP的中非南南合作信息交流平台网站建设
随机推荐
University of California | feasible confrontation robust reinforcement learning for unspecified environments
Real estate industry reshuffle
高效生成接口文档好方法
网络数据包多层传输演示
创意下拉多选js插件下载
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析
[Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
C# 合并集合
TypeError: ‘str‘ object is not callable的错误原因
蓝桥杯基础练习——矩阵的回形取数(C语言)
某公司网络设计与规划
[hdlbits questions] Verilog language (3) modules: hierarchy section
Code sharing of social chat platform developed by dating website (III)
哪吒 D1-H 测试 microbench
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板
ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
Istio exposes applications to the Internet