当前位置:网站首页>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();
*/边栏推荐
- University of California | feasible confrontation robust reinforcement learning for unspecified environments
- 重磅!《几何深度学习》新书发布,帝国理工/DeepMind等图ML大牛共同撰写,160页pdf阐述几何DL基础原理和统一框架
- 安全基础6 ---漏洞复现
- Wxss template style and WXS scripting language for wechat applet development
- TypeError: ‘str‘ object is not callable的错误原因
- [applet development] detailed explanation of host environment
- Hongmeng - Damiao computing Sketchpad - VIDEO
- [wp]ctfshow-web入门-爆破
- Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
- How many lines of code is appropriate for a function? Clean Code
猜你喜欢

微信小程序10-微搭模板

Why learn service grid istio

Introduction to web security ICMP testing and defense

The second "future Cup" knowledge map championship was officially launched

伺服驱动器在机器人上的研究与应用

IP地址的概念

聊聊接口性能优化的11个小技巧

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

Network packet multi-layer transmission demonstration

相机内参矩阵K和fov的相互转换
随机推荐
How many lines of code is appropriate for a function? Clean Code
Old wine in new bottles -- sample analysis of recent apt32 (sea Lotus) organizational attacks
哪吒 D1-H 测试 microbench
[applet development] common components and basic usage details
Wechat campus maintenance and repair application applet graduation design finished product of applet completion work (6) opening defense ppt
University of California | feasible confrontation robust reinforcement learning for unspecified environments
GBASE 8s UDR内存管理_02_mi_dalloc
Common misunderstandings caused by a time reporting assistant of Blue Bridge Cup basic questions
TypeError: ‘str‘ object is not callable的错误原因
Kcon 2022 highlights and agenda revealed!
Sccm2012r2 network deployment reinstallation system
Telnet installation and telnet (correct password) cannot log in!
Heavy! The new book "deep learning in geometry" was released, which was jointly written by imperial Institute of technology /deepmind and other figures ml Daniel. The 160 page PDF expounds the basic p
Introduction to web security ICMP testing and defense
创意下拉多选js插件下载
Hash undirected graph visualization
Leetcode skimming: dynamic programming 07 (different binary search trees)
What is the application value of MES management system
Fearless of high temperature and rainstorm, how can Youfu network protect you from worry?
telnet安装以及telnet(密码正确)无法登录!