当前位置:网站首页>leetcode-919:完全二叉树插入器
leetcode-919:完全二叉树插入器
2022-07-25 20:36:00 【菊头蝙蝠】
leetcode-919:完全二叉树插入器
题目
完全二叉树 是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。
设计一种算法,将一个新节点插入到一个完整的二叉树中,并在插入后保持其完整。
实现 CBTInserter 类:
CBTInserter(TreeNode root) 使用头节点为 root 的给定树初始化该数据结构;
CBTInserter.insert(int v) 向树中插入一个值为 Node.val == val的新节点 TreeNode。使树保持完全二叉树的状态,并返回插入节点 TreeNode 的父节点的值;
CBTInserter.get_root() 将返回树的头节点。
示例 1:

输入
["CBTInserter", "insert", "insert", "get_root"]
[[[1, 2]], [3], [4], []]
输出
[null, 1, 2, [1, 2, 3, 4]]
解释
CBTInserter cBTInserter = new CBTInserter([1, 2]);
cBTInserter.insert(3); // 返回 1
cBTInserter.insert(4); // 返回 2
cBTInserter.get_root(); // 返回 [1, 2, 3, 4]

解题
方法一:双队列—O(1)复杂度插入
如果每次插入的时候都进行层序遍历,那么如果在插入情况比较多的时候,效率会很低。
如果能直接从队列中取出节点,然后插入,那么就会快很多。
由于是完美二叉树,被插入的节点,只可能是2种状态
- 没有左、右子节点, 这种情况下后续是先插入左节点
- 有左子节点,没右子节点,这种情况下,插入右节点
初始化的时候将情况1,存储在q_L队列中,将情况2,存储在q_R队列中。
因此可以通过层序遍历,初始化q_L,q_R队列
由于完全二叉树的特性,插入的时候,优先从q_R队列中取值。
/** * 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* root;
queue<TreeNode*> q_L;//存放没有左右孩子的节点
queue<TreeNode*> q_R;//存放没有右孩子的节点
CBTInserter(TreeNode* root) {
//目的是为了初始化q_L和q_R队列
this->root=root;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* cur=q.front();
q.pop();
if(!cur->left&&!cur->right) q_L.push(cur);
if(cur->left&&!cur->right) q_R.push(cur);
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
}
int insert(int val) {
if(!q_R.empty()){
//优先取仅没有右孩子的节点(因为完全二叉树的特点)
TreeNode* cur=q_R.front();
q_R.pop();
cur->right=new TreeNode(val);
q_L.push(cur->right);
return cur->val;
}
else{
//取没有左右孩子的节点
TreeNode* cur=q_L.front();
q_L.pop();
cur->left=new TreeNode(val);
q_R.push(cur);
q_L.push(cur->left);
return cur->val;
}
}
TreeNode* get_root() {
return root;
}
};
方法二:插入时–层序遍历
class CBTInserter {
public:
TreeNode* root;
CBTInserter(TreeNode* root) {
this->root=root;
}
int insert(int val) {
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* cur=q.front();
q.pop();
if(cur->left) q.push(cur->left);
else{
cur->left=new TreeNode(val);
return cur->val;
}
if(cur->right) q.push(cur->right);
else{
cur->right=new TreeNode(val);
return cur->val;
}
}
return -1;
}
TreeNode* get_root() {
return root;
}
};
边栏推荐
- [advanced mathematics] [4] indefinite integral
- [paper reading] unpaired image to image translation using cycle consistent advantageous networks
- Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
- Kubernetes进阶部分学习笔记
- 【TensorRT】动态batch进行推理
- 文件操作详解
- Jmeter——接口测试
- 使用cookie登录百度网盘(网站使用cookie)
- [matlab] download originality documents based on oil monkey script and MATLAB
- Principle analysis of bootloader
猜你喜欢

【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)

雷达水位计的工作原理及安装维护注意事项
![[advanced mathematics] [5] definite integral and its application](/img/b2/62748b7533982f2b864148e0857490.png)
[advanced mathematics] [5] definite integral and its application

Online random coin tossing positive and negative statistical tool

Key network protocols in tcp/ip four layer model

Remote monitoring solution of intelligent electronic boundary stake Nature Reserve

【高等数学】【8】微分方程

Today's sleep quality record 75 points
![MySQL date [plus sign / +] condition filtering problem](/img/86/aed048e27b3e0b0baa919204bc067c.png)
MySQL date [plus sign / +] condition filtering problem

智能电子界桩自然保护区远程监控解决方案
随机推荐
JS作用域与作用域链
Do you still have certificates to participate in the open source community?
【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
Has baozi ever played in the multi merchant system?
每条你收藏的资讯背后,都离不开TA
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
Link list of sword finger offer question bank summary (III) (C language version)
TGA file format (waveform sound file format)
Go language go language built-in container
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
【TensorRT】trtexec工具转engine
Advantages of network virtualization of various manufacturers
Automated testing ----- selenium (I)
10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
2022.7.24-----leetcode.1184
Log in to Baidu online disk with cookies (websites use cookies)
Proxy implements MySQL read / write separation
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input