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 ...
随机推荐
- poj2154Color polya定理+欧拉函数优化
没想到贱贱的数据居然是错的..搞得我调了一中午+晚上一小时(哦不d飞LJH掉RP毕竟他是BUFF)结果重判就对了五次.. 回归正题,这题傻子都看得出是polya定理(如果你不是傻子就看这里),还没有翻 ...
- Get started with Sourcetree
Understand the interface Bookmarks window From that window, select the Local or Remote buttons to vi ...
- 【Android进度条】三种方式实现自定义圆形进度条ProgressBar
一.通过动画实现 定义res/anim/loading.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- 并不对劲的bzoj4651:loj2086:uoj222:p1712:[NOI2016]区间
题目大意 有\(n\)(\(n\leq 5*10^5\))个闭区间\([L_1,R_1],[L_2,R_2],...,[L_n,R_n]\)(\(\forall i\in [1,n],0\leq L_ ...
- Goland软件使用教程(二)
Goland软件使用教程(二)一.编码辅助功能 1. 智能补全 IDE通过自动补全语句来帮助您来编写代码.快捷键“Ctrl+shift+空格”将会给你一个在当前上下文中最相关符号的列表,当您 ...
- 《The Unreasonable Effectiveness of Recurrent Neural Networks》阅读笔记
李飞飞徒弟Karpathy的著名博文The Unreasonable Effectiveness of Recurrent Neural Networks阐述了RNN(LSTM)的各种magic之处, ...
- Mysql数据库介绍、安装和配置文件
Mysql数据库介绍.安装和配置文件 MySQL数据库介绍 mysql是开源关系型数据库,遵循GPL协议. mysql的特点是性能卓越且服务稳定,开源,无版本限制,成本低,单进程多线程,多用户,基于C ...
- POJ3682;King Arthur's Birthday Celebration(期望)
传送门 题意 进行翻硬币实验,若k次向上则结束,进行第n次实验需花费2*n-1的费用,询问期望结束次数及期望结束费用 分析 我们令f[i]为结束概率 \[f[i]=C_{i-1}^{k-1}*p^k* ...
- poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...
- IIS重新注册
打开程序-运行-cmd:输入一下命令重新注册IISC:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i