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. 扩展KMP(占位)

    先放代码. #include<cstdio> #include<iostream> #include<cstring> using namespace std; ; ...

  2. POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)

    Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the ...

  3. Integrate Your Code with the Frameworks---整合你的代码和框架

    Back to Frameworks Integrate Your Code with the Frameworks When you develop an app for OS X or iOS, ...

  4. linux的僵尸进程和孤儿进程

    1 僵尸进程: 子进程已经退出勒 但是还没有回收资源的进程为僵尸进程 代码验证 #include <stdio.h> #include <stdlib.h> #include ...

  5. SCUT - 261 - 对称与反对称 - 构造 - 简单数论

    https://scut.online/p/261 由于M不是质数,要用扩展欧几里得求逆元,而不是费马小定理! 由于M不是质数,要用扩展欧几里得求逆元,而不是费马小定理! 由于M不是质数,要用扩展欧几 ...

  6. Thrift 原理与使用实例

    一.Thrift 框架介绍 1.前言 Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目.Thrift通过一个中间语言(IDL, 接口定 ...

  7. POJ3250【单调栈】

    思路: 维护一个单调递增的栈,对于栈顶元素<=新值来说,那么后面的,我一定看不到了,pop掉以后,那么这时候的栈的大小就是我能看到的这个刚刚pop出去元素的个数. //#include < ...

  8. POJ 1384【完全背包】

    题意: 已知储蓄罐满时的质量f以及空时质量e, 有n种硬币,每种硬币的价值为p,质量为w, 求该储蓄罐中的最少有多少钱? 思路: 完全背包思想,问题是在一个重量下的最小价值 那么只要变一下符号就好了? ...

  9. bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】

    第一问就是Σ(deg[u]-1)/2+1 第二问是二分,判断的时候考虑第一问的贪心规则,对于奇度数的点,两两配对之后一条延伸到上面:对于欧度数的点,两两配对或者deg[u]-2的点配对,然后一条断在这 ...

  10. mysqldump 工具使用详解——参数选项

    mysqldump 简介 mysqldump 是一种用于逻辑备份的客户端工具,它会产生一套能够重新构建数据库或表的SQL语句.所谓逻辑备份:是利用SQL语言从数据库中抽取数据并存于二进制文件的过程.逻 ...