当前位置:网站首页>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();
*/边栏推荐
- binarySearch基础二分查找
- Wechat campus maintenance and repair applet graduation design finished product (5) assignment of applet completion work
- C# 合并集合
- Network design and planning of a company
- [applet development] do you know about applet development?
- Wechat campus maintenance and repair applet graduation design finished product of applet completion work (4) opening report
- 基于海思3559 高效率的 0延时 0拷贝 qt播放器方案
- Actual combat of MySQL database design project of online mall system
- What is the application value of MES management system
- KCon 2022 亮点及议程大揭秘!
猜你喜欢

Flutter 小技巧之优化你使用的 BuildContext

六轴传感器使用学习记录

帝国CMS7.5仿《问答库》题库问答学习平台网站源码 带手机版
![[hdlbits questions] Verilog language (3) modules: hierarchy section](/img/35/ccdbb55aa0aff7e9dec2bf9e64c4e2.png)
[hdlbits questions] Verilog language (3) modules: hierarchy section

相机内参矩阵K和fov的相互转换

安全基础6 ---漏洞复现

Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt

安全基础4 ---正则表达式
![[iniparser] simple use of the project configuration tool iniparser](/img/2b/1d20b4ef44dfe2544891d9c72b676e.png)
[iniparser] simple use of the project configuration tool iniparser

Imperial cms7.5 imitation "question and answer library" question and answer learning platform website source code with mobile version
随机推荐
[wp]ctfshow-web入门-爆破
C merge set
微信小程序 26 播放音乐页的完善②
AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
Intouch高级报警(报警筛选)
由一个蓝桥杯基础题报时助手而引出的常见误区
Flutter 小技巧之优化你使用的 BuildContext
Amrita Institute of Engineering | reinforcement active learning method for optimizing sampling in terms extraction of emotional analysis
Hash undirected graph visualization
Global configuration and page configuration of wechat applet development
Scala foundation [set 01]
Improvement of wechat applet 29 hot search list ②
iMeta | Sangerbox: 交互式整合临床生信分析平台
[Detr for 3D object detection] detr3d: 3D object detection from multi view images via 3D-to-2D queries
Flutter tips: optimizing the buildcontext you use
前夕 - 0day威胁情报
How to analyze qiime2 after obtaining picrust2 results
Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
Modelsim and quartus jointly simulate PLL FIFO and other IP cores
Code sharing of social chat platform developed by dating website (III)