当前位置:网站首页>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();
*/边栏推荐
- Hongke shares | how to solve blackmail software security vulnerabilities
- 【刷题记录】21. 合并两个有序链表
- 前夕 - 0day威胁情报
- Network data request for wechat applet development
- 网络数据包多层传输演示
- JS learning notes 17: DOM query exercise
- 哈希无向图可视化
- TFIDF examples and explanations
- 阿姆利塔工程学院|用于情感分析的方面术语提取中优化采样的强化主动学习方法
- [Detr for 3D object detection] 3detr: an end to end transformer model for 3D object detection
猜你喜欢

小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能

KCon 2022 亮点及议程大揭秘!

Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt
![[hdlbits questions] Verilog language (3) modules: hierarchy section](/img/35/ccdbb55aa0aff7e9dec2bf9e64c4e2.png)
[hdlbits questions] Verilog language (3) modules: hierarchy section

Network packet multi-layer transmission demonstration

How to analyze qiime2 after obtaining picrust2 results
![[wp]ctfshow-web入门信息搜集](/img/22/c2e5cca918800dda9df27272eb9871.png)
[wp]ctfshow-web入门信息搜集

Hongmeng - Damiao computing Sketchpad - Introduction

Internal network planning and design of Yingcheng hospital

Pymoo learning (6): termination conditions
随机推荐
AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
Improvement of wechat applet 26 playing music page ②
【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
Imeta | sangerbox: interactive integrated clinical information analysis platform
高效生成接口文档好方法
GBASE 8s UDR内存管理_03_mi_realloc
Talk about 15 tips of SQL optimization
滑雪手机端H5小游戏源码下载
Wechat campus maintenance and repair applet graduation design finished product (7) Interim inspection report
Youfu network was invited to attend the 2022 national CIO conference and won the title of "CIO trusted brand"
Actual combat of MySQL database design project of online mall system
【DETR用于3D目标检测】3DETR: An End-to-End Transformer Model for 3D Object Detection
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Day7:有序二叉树(二叉搜索树)
KCon 2022 亮点及议程大揭秘!
蓝桥杯基础练习——矩阵的回形取数(C语言)
微信小程序 28 热搜榜的完善①
给容器添加3d效果的副标题
Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
[wp]ctfshow-web入门信息搜集