当前位置:网站首页>类中多函数填写,LeetCode919——完全二叉树插入器
类中多函数填写,LeetCode919——完全二叉树插入器
2022-07-28 21:51:00 【dor.yang】
题目内容
完全二叉树 是每一层(除最后一层外)都是完全填充(即,节点数达到最大)的,并且所有的节点都尽可能地集中在左侧。
设计一种算法,将一个新节点插入到一个完整的二叉树中,并在插入后保持其完整。
实现 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]
提示:
树中节点数量范围为 [1, 1000]
0 <= Node.val <= 5000
root 是完全二叉树
0 <= val <= 5000
每个测试用例最多调用 insert 和 get_root 操作 104 次
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/complete-binary-tree-inserter
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
其实如果但是说这个题目意思的话,其实还是好理解的,就是一个完全二叉树+节点的问题,但是麻烦的地方一个是二叉树的构建上,他们提供了一棵二叉树,但是我在一开始理解初始化函数功能的时候出现了偏差,以为是要我们自己构建出一个完全二叉树来,这点我尝试实现了一下,效果其实并不好。
后来看了题解的思路我才反应过来初始化函数的功能其实并没有什么明确的限制,只是为之后的函数服务的罢了。只要初始化的时候把所有叶子结点和可能存在的一个只有左节点没有右节点的节点存在队列里面。然后去一个出来连接上就是二叉树的插入了。至于返回父节点,我们找到节点的过程就输出了。
这是我第一个在力扣上做这种多个函数类型的题目,感觉还是有一些不自然,之后多练练希望可以好一些,就像昨天周赛的时候一样,遇到多个函数自己写类的题目,因为缺乏经验而无从下手,所以还是得加紧练习才行。
实现代码
/** * 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 {
private:
TreeNode* node;
queue<TreeNode*> link;
public:
CBTInserter(TreeNode* root) {
node=root;
queue<TreeNode*> q;
if(!root){
node=nullptr;
}
q.push(root);
while(!q.empty()){
TreeNode* dian=q.front();
q.pop();
if(dian->left){
q.push(dian->left);
}
if(dian->right){
q.push(dian->right);
}
if(!dian->left||!dian->right){
link.push(dian);
}
}
}
int insert(int val) {
TreeNode* dian=new TreeNode(val);
TreeNode* mid=link.front();
if(mid->left){
mid->right=dian;
link.pop();
link.push(dian);
}
else{
mid->left=dian;
link.push(dian);
}
return mid->val;
}
TreeNode* get_root() {
return node;
}
};
/** * 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(); */
边栏推荐
- 二舅火了,全网刷屏,他凭什么能治好我的精神内耗?
- In order for digital retail to continue to play its role, we need to give new connotation and significance to digital retail
- 深度剖析集成学习Xgboost(续)
- 金仓数据库 KingbaseES与Oracle的兼容性说明(2. 数据类型)
- 零念科技完成Pre-A轮融资,推动智能驾驶平台软件国产替代
- My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?
- 使用这个,你发的消息就无法被监控了
- 当初的“你“为什么做测试/开发程序员?自己存在的价值......
- 顶级“黑客”能厉害到什么地步?无信号也能上网,专家:高端操作!
- CV实例分割模型小抄(1)
猜你喜欢
Byte 8 years' experience of female test Director - for girls who want to change careers or are about to enter the testing industry
mongodb索引添加、查看、导出、删除
Wechat applet development ③
1314_串口技术_RS232通信基础的信息
Trivy [3] custom scanning strategy
Runloop principle (II)
深开鸿:万物智联的大江上,升起一轮开源鸿蒙月
搭载新一代超安全蜂窝电池,思皓爱跑上市13.99万元起售
2022t elevator repair examination questions and simulation examination
What if win11 cannot find the DNS address? Win11 can't find DNS and can't access the web page solution
随机推荐
Price for volume has encountered "six consecutive declines" in sales. Can Volvo, which is no longer safe, turn around?
Form label
1314_ Serial port technology_ Basic information of RS232 communication
Development of small programs ①
The functions and differences of display, visibility and overflow
How does VR panorama entrepreneurship expand the market? How to make the road of entrepreneurship smoother?
Optimization and implementation of custom MVC
【自】-刷题-BFS
2022焊工(初级)上岗证题目及答案
2022g3 boiler water treatment test simulation 100 questions simulation test platform operation
刨根问底学 二叉树
22 Niu Ke multi school Day1 J - serval and essay heuristic merging
1314_串口技术_RS232通信基础的信息
Routeros limited DNS hijacking and check
22牛客多校day1 I - Chiitoitsu 概论dp
[self] - question brushing - dynamic programming
2022 G2 power plant boiler stoker examination question bank simulated examination platform operation
顶级“黑客”能厉害到什么地步?无信号也能上网,专家:高端操作!
[self] - question brushing - peak value
Why did "you" become a test / development programmer? The value of your existence