leetcode_919. Complete Binary Tree Inserter
https://leetcode.com/problems/complete-binary-tree-inserter/
设计一个CBTInserter,使用给定完全二叉树初始化。三个功能;
CBTInserter(TreeNode root)
initializes the data structure on a given tree with head noderoot
;CBTInserter.insert(int v)
will insert aTreeNode
into the tree with valuenode.val = v
so that the tree remains complete, and returns the value of the parent of the insertedTreeNode
;CBTInserter.get_root()
will return the head node of the tree.
主要涉及完全二叉树的插入。
解法一:dfs based
对给定的完全二叉树,先计算其最大高度maxlayer,以及深度最深的节点数numoflastlayer。如果numoflastlayer<2^(maxlayer-1),说明应该在maxlayer-1层第一个子节点数小于2的节点插入;如果numoflastlayer==2^(maxlayer-1),说明应该在maxlayer层第一个子节点处插入,利用dfs可以完成。插入过后及时更新maxlager和numoflastlayer。
class CBTInserter{
public:
void preorder(TreeNode* root,int layer){
if(layer>maxlayer){
maxlayer = layer;
numoflastlayer = ;
}else if(layer == maxlayer)
numoflastlayer++;
if(root->left!=NULL)
preorder(root->left, layer+);
if(root->right!=NULL)
preorder(root->right, layer+);
}
CBTInserter(TreeNode* root){
root_ =root;
maxlayer=-;
numoflastlayer=;
preorder(root,);
}
TreeNode* Insert(TreeNode* root, int v, int layer, int insertlayer){
if(layer == insertlayer-){
if(root->left == NULL){
root->left = new TreeNode(v);
return root;
}else if(root->right == NULL){
root->right = new TreeNode(v);
return root;
}
}else{
TreeNode* res = Insert(root->left, v, layer+, insertlayer);
if(res == NULL)
res = Insert(root->right, v, layer+, insertlayer);
return res;
}
return NULL;
}
int insert(int v){int maxnumoflastlayer = pow(, maxlayer);
TreeNode* res = NULL;
if(numoflastlayer<maxnumoflastlayer){
res = Insert(root_,v,, maxlayer);
numoflastlayer++;
}else{
res = Insert(root_,v,,maxlayer+);
maxlayer++;
numoflastlayer=;
}
return res->val;
}
TreeNode* get_root(){
return root_;
}
private:
TreeNode* root_;
int maxlayer;
int numoflastlayer;
};
解法二:bfs based
先使用bfs将所有子节点数为0和1的节点存入队列,然后维护这个队列,对头节点是插入新节点的节点,若对头节点只有右子树为NULL,那么插入后将其pop,并将其两个子节点指针压入队列。
class CBTInserter{
public:
TreeNode* root_;
queue<TreeNode*> nodes_0_1;
CBTInserter(TreeNode* root){
root_ = root;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
TreeNode* now = que.front();
que.pop();
if(now->left == NULL)
nodes_0_1.push(now);
else if(now->right == NULL)
nodes_0_1.push(now);
else{
que.push(now->left);
que.push(now->right);
}
}
}
int insert(int v){
TreeNode* root = nodes_0_1.front();
if(root->left!=NULL){
root->right = new TreeNode(v);
nodes_0_1.pop();
nodes_0_1.push(root->left);
nodes_0_1.push(root->right);
}
else
root->left = new TreeNode(v);
return root->val;
}
TreeNode* get_root(){
return root_;
}
};
leetcode_919. Complete Binary Tree Inserter的更多相关文章
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- LeetCode 919. Complete Binary Tree Inserter
原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- leetcode_919. Complete Binary Tree Inserter_完全二叉树插入
https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- A1110. Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
随机推荐
- leetcode 660. Remove 9
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- Can't remove netstandard folder from output path (.net standard)
https://developercommunity.visualstudio.com/content/problem/30940/cant-remove-netstandard-folder-fro ...
- mysql查询表的字符集
mysql查询表的字符集 SHOW CREATE TABLE user;
- C语言预处理命令总结大全 :宏定义
C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...
- BZOJ1854:游戏(二分图匹配)
lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...
- 如何编写linux下nand flash驱动-4
2. 软件方面 如果想要在Linux下编写Nand Flash驱动,那么就先要搞清楚Linux下,关于此部分的整个框架.弄明白,系统是如何管理你的nand flash的,以及,系统都帮你做 ...
- asp.net mvc razer
Asp.net MVC Razor模板引擎技巧分享 http://www.cnblogs.com/JustRun1983/p/3545303.html 全新的membership框架Asp.net I ...
- asp.net 中的事务
ASP.NET开发过程中的事务处理 http://www.cnblogs.com/georgeHeaven/p/3766609.html
- 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】
用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...
- 洛谷P4216 [SCOI2015]情报传递(树剖+主席树)
传送门 我们可以进行离线处理,把每一个情报员的权值设为它开始收集情报的时间 那么设询问的时间为$t$,就是问路径上有多少个情报员的权值小于等于$t-c-1$ 这个只要用主席树上树就可以解决了,顺便用树 ...