leetcode_919. Complete Binary Tree Inserter_完全二叉树插入
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_完全二叉树插入的更多相关文章
- [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- 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 ...
- leetcode_919. Complete Binary Tree Inserter
https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
随机推荐
- 理解Objective-C Runtime (六)super
super 在Objective-C中,如果我们需要在类的方法中调用父类的方法时,通常都会用到super,如下所示: @interface MyViewController: UIViewContro ...
- [SDOI2016] 模式字符串 (BZOJ4598 & VIJOS1995)
首先直接点分+hash就可以做,每个点用hash判断是否为S重复若干次后的前缀或后缀,每个子树与之前的结果O(m)暴力合并.在子树大小<m时停止分治,则总复杂度为O(nlog(n/m)). 问题 ...
- javascript之存储数据-cookie,localStorage,sessionStorage
cookie: 存储一些简单的数据,以文本形式放到本地,大小4kb 存储:document.cookie='name=value' 取值:document.cookie(字符串) 期限:expires ...
- 分享Ubuntu下一些很棒的软件(一)
分享一些我在Ubuntu下常用的软件. Goolge Chrome/Firefox/Thunderbird这些重量级的跨平台的软件虽然很强大,但大家应该都比较熟悉了,没有太多必要在这里介绍.本文涉及到 ...
- Linux网络编程笔记(修订版)
我的网络编程笔记, 因为最近又要做Linux下的网络编程,故重新修订, 其中一些内容参考了文末的链接及文章 1. 基本概念 2. 基本接口 2.1. 打开一个socket 2.2. 将 ...
- 微信小程序在线支付功能使用总结
最近需要在微信小程序中用到在线支付功能,于是看了一下官方的文档,发现要在小程序里实现微信支付还是很方便的,如果你以前开发过服务号下的微信支付,那么你会发现其实小程序里的微信支付和服务号里的开发过程如出 ...
- COCOS2DX场景切换特效
cocos2d-x 3.0中场景切换特效比较多,而且游戏开发中也经常需要用到这些特效,来使场景切换时不至于那么干巴,遂这里汇总一下,开发中使用. 百牛信息技术bainiu.ltd整理发布于博客园 场景 ...
- 【旧文章搬运】超级无敌大炉子的LzOpenProcess
原文发表于百度空间,2008-11-20========================================================================== 这个东西嘛 ...
- B. Spider Man
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- (水题)洛谷 - P1478 - 陶陶摘苹果(升级版)
https://www.luogu.org/problemnew/show/P1478 没啥好说的…… 居然还漏写一个等于号WA了一发. #include<bits/stdc++.h> u ...