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

给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能。完全二叉树的定义为:这颗二叉树除最后一层外左右层的节点都是满的(对于第i层有2^(i-1)个节点),最后一层节点都出现在尽量靠左的位置。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class CBTInserter {
public:
CBTInserter(TreeNode* root) {}
int insert(int v) {}
TreeNode* get_root() {}
};
/**
* Your CBTInserter object will be instantiated and called as such:
* CBTInserter* obj = new CBTInserter(root);
* int param_1 = obj->insert(v);
* TreeNode* param_2 = obj->get_root();

解法一:

我的思路是,先遍历给定二叉树,得到树的深度maxlayer和最后一层的节点数numoflastlayer。若numoflastlayer<2^(maxlayer-1),则新节点插入在第layer=maxlayer层;若numoflastlayer=2^(maxlayer-1),说明最后一层满了,需要插入到第layer=maxlayer+1层。再先序遍历这棵树,遍历到第layer-1层的节点时,判断:若节点只有左节点,则将新节点作为其右孩子;若节点无子节点,则将新节点作为其左孩子。

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)
{
cout<<v<<endl;
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;
};

解法二:新节点插入的位置的父节点的子节点数要么为1,要么为0。根据层次遍历,把节点子节点数为0或1的节点存入队列。插入时,取队头节点,若该节点有左孩子,则将新节点作为其右孩子,并将该左右孩子压入队尾,将该队头节点出队;若该节点无孩子,则将新节点作为其左孩子,该节点仍然作为队头节点。

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. [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器

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

  2. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  3. leetcode_919. Complete Binary Tree Inserter

    https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...

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

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

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

  6. [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)

    1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...

  7. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

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

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

  9. PAT1110:Complete Binary Tree

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

随机推荐

  1. BZOJ_4278_[ONTAK2015]Tasowanie_后缀数组

    BZOJ_4278_[ONTAK2015]Tasowanie_后缀数组 Description 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. Input ...

  2. [POI 2014] Couriers

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3524 [算法] 首先离线 , 将询问按右端点排序 如果我们知道[l , r]这个区间 ...

  3. HBase之四--(2):spring hadoop 访问hbase

    1.  环境准备: Maven Eclipse Java Spring 2. Maven  pom.xml配置 <dependency> <groupId>org.apache ...

  4. node npm 总结

    是nodejs的软件包管理器,用于node插件管理 npm install <name> [-g] [--save -dev] name:安装模块的名称 -g:全局安装 --save:将保 ...

  5. SPOJ FIBOSUM && FIBOSUM2

    Fibonacci数列定义为 $$f_n = f_{n-1}+f_{n-2}, \text{以及初值}f_0=0, f_1=1.$$ 本文之讨论,皆在模$10^9+7$意义下. FIBOSUM 给定$ ...

  6. sublime text 3中修改tab键为缩进4个空格

    1. 菜单栏里点击 Preferences-> Setting-User, 如图 2. 在弹出来的文本里,添加如下两行: { // 注意只有一个大括号,如果之前有属性,如在之前的属性后确保有 , ...

  7. View Controller Programming Guide for iOS---(三)---Using View Controllers in Your App

    Using View Controllers in Your App Whether you are working with view controllers provided by iOS, or ...

  8. LXD安装

    #安装 #初始化(一路next) sudo lxd init #启动容器 lxc launch ubuntu:16.04 first #进到容器内 lxc exec first -- /bin/bas ...

  9. 洛谷 - P5000 - Hillwer编码 - 高精度

    https://www.luogu.org/problemnew/show/P5000 第一次写一个正经的高精度题. 很明显ASCII码的乘积绝对是溢出的. 那么直接上Java.正好学一手Java的字 ...

  10. java实现数据结构

    数据结构与算法 :一.数据结构和算法简介 数据结构是指数据在计算机存储空间中的安排方式,而算法时值软件程序用来操作这些结构中的数据的过程.二. 数据结构和算法的重要性 几乎所有的程序都会使用到数据结构 ...