https://leetcode.com/problems/complete-binary-tree-inserter/

设计一个CBTInserter,使用给定完全二叉树初始化。三个功能;

  • CBTInserter(TreeNode root) initializes the data structure on a given tree with head node root;
  • CBTInserter.insert(int v) will insert a TreeNode into the tree with value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode;
  • 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的更多相关文章

  1. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  2. [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  3. LeetCode 919. Complete Binary Tree Inserter

    原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/ 题目: A complete binary tree is a ...

  4. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  5. leetcode_919. Complete Binary Tree Inserter_完全二叉树插入

    https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...

  6. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. A1110. Complete Binary Tree

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  8. 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 ...

  9. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

随机推荐

  1. ios UIWebView 播放优酷土豆视频

    将以下的代码嵌套在html里.然后webView载入这个网页.或这段html码,即可了,无须要使用像网上说的html5去兼容 watermark/2/text/aHR0cDovL2Jsb2cuY3Nk ...

  2. caioj1097: [视频]树状数组1(快速求和计算) cdq分治入门

    这题虽然是个树状数组,但是也可以用cdq分治做啊~~,这个就是一个浅显的二维偏序的应用? cdq分治和普通的分治有什么区别? 举个栗子:有4个小朋友,你请他们吃饭,假如你分治搞,就会分成很多子问题—— ...

  3. 【转】WdatePicker.js的使用方法 帮助文档 使用说明 如何使用

    [转]WdatePicker.js的使用方法 帮助文档 使用说明 如何使用 日期控件支持平面显示功能,只要设置一下eCont属性就可以把它当作日历来使用了,无需触发条件,直接显示在页面上 示例2-1 ...

  4. 《Visual C++ 2010入门教程》系列四:VC2010中初学者常见错误、警告和问题

    <Visual C++ 2010入门教程>系列四:VC2010中初学者常见错误.警告和问题   这一章将帮助大家解释一些常见的错误.警告和问题,帮助大家去理解和解决一些常见问题,并了解它的 ...

  5. 并不对劲的bzoj4868: [Shoi2017]期末考试

    传送门-> 三分裸题. 发现答案只和最后一个科目公布成绩的时间有关. 科目公布成绩的时间越早,老师们的不愉快度越高,这个时间越晚,学生们的不愉快度越高. 它看上去像个单峰的,那么就三分好了. 判 ...

  6. BZOJ_2821_作诗(Poetize)_分块

    BZOJ_2821_作诗(Poetize)_分块 Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要 ...

  7. UI:简单的SQL语句

    一.SQL语句如果要在程序运行过程中操作数据库中的数据,那得先学会使用SQL语句1.什么是SQLSQL(structured query language):结构化查询语言SQL是一种对关系型数据库中 ...

  8. 1.Ext.onReady详解

    转自:https://blog.csdn.net/xufang461010923/article/details/37660139 我们知道,只有在Ext框架全部加载完后才能在客户端的代码中使用Ext ...

  9. 虚拟机安装hadoop

    1.用VMware建立两台虚拟机(Centos6.5)系统,并设立主机名为node1与node2 2.设置虚拟机网络两台都是设置为如图: 3.修改两台虚拟机的/etc/hosts的文件为 (其中192 ...

  10. JavaScript 数组相关基础方法

    文章来源于:https://www.cnblogs.com/dolphinX/p/3353590.html 创建数组 构造函数 1.无参构造函数,创建一空数组 var a1=new Array(); ...