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 ...
随机推荐
- ABAP文件加密解密-PGP
1.SM69创建命令 2.解密 DATA: lv_para = '--passphrase (key) -o /oracle/sfdata/sfdata.csv -d /oracle/sfdata/s ...
- Lightoj 1017 - Brush (III)
1017 - Brush (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sam ...
- POJ1182 食物链 —— 种类并查集
题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- mac系统下如何删除银行安全插件
要分类解决了,一般safair插件都是pkg包安装的 如果:1.制作者够良心,PKG安装包中植入了删除卸载功能,那就好办了,打开当时安装的pkg包,执行删除选项 2.没良心的,装了不好删的,我目前个人 ...
- 给Xcode增加复制行、删除行快捷键的方法
运行: sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKey ...
- 2-4 测试案例helloWorld
- bzoj 3105: [cqoi2013]新Nim游戏【线性基+贪心】
nim游戏的先手必胜条件是所有堆的火柴个数异或和为0,也就是找一个剩下火柴堆数没有异或和为0的子集的方案,且这个方案保证剩下的火柴个数总和最大 然后我就不会了,其实我到现在也不知道拟阵是个什么玩意-- ...
- bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】
算法详见:http://www.cnblogs.com/lokiii/p/8191573.html 求出点两两之间的最小割之后,把他们扔到map/set里跑即可 可怕的是map和set跑的时间竟然完全 ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- 第四章vs2107 代码实际运用-后台权限管理讲解 创建角色
先看一下项目整体结构图: 实体类和数据操作都在前面用TT模板批量生产了.下面开始介绍权限代码这块的逻辑. 创建角色开始. 1. 角色的创建我们用到三张表 A.menuinfo(菜单表) role(角 ...