当前位置:网站首页>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();
*/边栏推荐
- What is the application value of MES management system
- Security foundation 6 - vulnerability recurrence
- [reading notes] deep learning Chapter 1: Introduction
- 某公司网络设计与规划
- 帝国CMS整站|手机号/QQ靓号商城源码|适配移动端
- Code sharing of social chat platform developed by dating website (III)
- How to analyze qiime2 after obtaining picrust2 results
- C# 合并集合
- 创意下拉多选js插件下载
- [iniparser] simple use of the project configuration tool iniparser
猜你喜欢

Actual combat of MySQL database design project of online mall system

Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks

Scala基础【集合01】

Basic practice of Blue Bridge Cup - shape retrieval of matrix (C language)

Network data request for wechat applet development

微信小程序开发之全局配置与页面配置

微信小程序 27 进度条的动态实现和搜索框、热搜榜的静态搭建

JS basic type reference type deep / shallow clone copy

哪吒 D1-H 测试 microbench

binarySearch基础二分查找
随机推荐
[Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
Wechat campus maintenance application applet graduation design finished product of applet completion work (8) graduation design thesis template
GBASE 8s UDR内存管理_03_mi_realloc
高效生成接口文档好方法
小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板
The finished product of wechat campus maintenance and repair applet graduation design (1) development outline
Talk about 15 tips of SQL optimization
Swift 基础 Codable(JSONEncoder JSONDecoder)的使用
Use of swift basic codable (jsonencoder jsondecoder)
虹科分享|如何解决勒索软件安全漏洞
Scala基础【集合01】
JS learning notes 16: switching pictures small project practice
Improvement of wechat applet 26 playing music page ②
一个函数中写多少行代码比较合适呢? 代码整洁之道
某公司网络设计与规划
Pymoo learning (7): Parallelization
【HDLBits 刷题】Verilog Language(3)Modules: Hierarchy 部分
TypeError: ‘str‘ object is not callable的错误原因
Monitor MySQL based on MySQL exporter
GBASE 8s UDR内存管理_01_mi_alloc