当前位置:网站首页>919. 完全二叉树插入器
919. 完全二叉树插入器
2022-07-25 19:29:00 【anieoo】
原题链接:919. 完全二叉树插入器
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) { //建立完全二叉树
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; //结点在堆中的位置
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();
*/边栏推荐
- Binary tree visualization
- Amrita Institute of Engineering | reinforcement active learning method for optimizing sampling in terms extraction of emotional analysis
- ERROR: role “admin“ cannot be dropped because some objects depend on itDETAIL:
- Scala基础【集合01】
- 高效生成接口文档好方法
- Network design and planning of a company
- [server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage
- Why learn service grid istio
- Is there a "fingerprint" in the structure of AAAI 2022 | Gan? Generating network structure from forged image traceability
- 新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析
猜你喜欢

新瓶装老酒--近期APT32(海莲花)组织攻击活动样本分析

蓝桥杯基础练习——矩阵的回形取数(C语言)

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

IP地址的概念

Flutter tips: optimizing the buildcontext you use

JS basic type reference type deep / shallow clone copy

Actual combat of MySQL database design project of online mall system

How to be a self disciplined person?

Binary tree visualization

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
随机推荐
AAAI 2022 | GAN的结构有“指纹”吗?从伪造图像溯源生成网络结构
Amrita Institute of Engineering | reinforcement active learning method for optimizing sampling in terms extraction of emotional analysis
Scala foundation [set 01]
[server data recovery] a data recovery case of a brand ProLiant server raid paralysis, database file loss, and database file backup damage
微信小程序开发之WXSS模板样式与WXS脚本语言
KCon 2022 亮点及议程大揭秘!
Openresty Lua resty mlcache multi-level cache
JS learning notes 16: switching pictures small project practice
Hash undirected graph visualization
Code sharing of social chat platform developed by dating website (III)
Introduction of this course (Introduction to machine learning)
Intouch高级报警(报警筛选)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(5)任务书
某公司网络设计与规划
Leetcode skimming: dynamic programming 07 (different binary search trees)
Youfu force supercomputing provides customized high-performance computing services for customers
【DETR用于3D目标检测】DETR3D: 3D Object Detection from Multi-view Images via 3D-to-2D Queries
Eve - 0day Threat Intelligence
What is the application value of MES management system
Js分页插件支持表格、列表、文本、图像