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 ...
随机推荐
- mongo14-----group,aggregate,mapReduce
group,aggregate,mapReduce 分组统计: group() 简单聚合: aggregate() 强大统计: mapReduce() db.collection.group(docu ...
- YTU 2896: J--Zipper
2896: J--Zipper 时间限制: 1 Sec 内存限制: 128 MB 提交: 29 解决: 15 题目描述 Given three strings, you are to determ ...
- 关于追踪qemu 源码函数路径的一个方法
这阵子一直在研究qemu 磁盘io路径的源码,发现直接看代码是意见非常低效率的事情,qemu是一个比较庞大的家伙(源码部分大概154MB,完全由C语言来完成),整个结构也都非常地复杂,所以从代码上研究 ...
- mac系统下安装mysql步骤
1.下载mysql-5.7.13-osx10.11-x86_64.dmg安装包,并点击dmg安装包进行安装 2.安装完成后弹出如以下提示信息: 2016-06-23T01:14:48.649253Z ...
- 协议森林02 小喇叭开始广播 (以太网与WiFi协议)
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. “小喇叭开始广播啦”,如果你知道这个,你一定是老一辈的人.“小喇叭”是五十年代到八十年代的儿童广播 ...
- gcc编译系统
一. C语言编译过程 C语言的编译过程可分为四个阶段: 1.预处理(Preprocessing) 对源程序中的伪指令(即以#开头的指令)和特殊符号进行处理的过程. 伪指令包括:1)宏定义指令: 2)条 ...
- 杂项-Java:JBoss
ylbtech-杂项-Java:JBoss 是一个基于J2EE的开放源代码的应用服务器. JBoss代码遵循LGPL许可,可以在任何商业应用中免费使用.JBoss是一个管理EJB的容器和服务器,支持E ...
- Linux 常用命令六 cp和mv
一.cp命令 Linux中的复制命令. 复制文件: wang@wang:~/workpalce/python$ tree . ├── .txt ├── dir └── module directori ...
- 洛谷P4344 [SHOI2015]脑洞治疗仪(珂朵莉树)
传送门 看到区间推倒……推平就想到珂朵莉树 挖脑洞直接assign,填坑先数一遍再assign再暴力填,数数的话暴力数 //minamoto #include<iostream> #inc ...
- app 后台程序设计
限制客户端一分钟之内访问接口的次数 1.设备的唯一标识获取这个实际上IOS7后会存在问题,权限已经收回了,android可以2.唯一标识可以通过生成一个token区分3.每分钟的频率可以这样设置 ke ...