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。

  1. class CBTInserter{
  2. public:
  3. void preorder(TreeNode* root,int layer){
  4. if(layer>maxlayer){
  5. maxlayer = layer;
  6. numoflastlayer = ;
  7. }else if(layer == maxlayer)
  8. numoflastlayer++;
  9. if(root->left!=NULL)
  10. preorder(root->left, layer+);
  11. if(root->right!=NULL)
  12. preorder(root->right, layer+);
  13. }
  14. CBTInserter(TreeNode* root){
  15. root_ =root;
  16. maxlayer=-;
  17. numoflastlayer=;
  18. preorder(root,);
  19. }
  20. TreeNode* Insert(TreeNode* root, int v, int layer, int insertlayer){
  21. if(layer == insertlayer-){
  22. if(root->left == NULL){
  23. root->left = new TreeNode(v);
  24. return root;
  25. }else if(root->right == NULL){
  26. root->right = new TreeNode(v);
  27. return root;
  28. }
  29. }else{
  30. TreeNode* res = Insert(root->left, v, layer+, insertlayer);
  31. if(res == NULL)
  32. res = Insert(root->right, v, layer+, insertlayer);
  33. return res;
  34. }
  35. return NULL;
  36. }
  37. int insert(int v){int maxnumoflastlayer = pow(, maxlayer);
  38. TreeNode* res = NULL;
  39. if(numoflastlayer<maxnumoflastlayer){
  40. res = Insert(root_,v,, maxlayer);
  41. numoflastlayer++;
  42. }else{
  43. res = Insert(root_,v,,maxlayer+);
  44. maxlayer++;
  45. numoflastlayer=;
  46. }
  47. return res->val;
  48. }
  49. TreeNode* get_root(){
  50. return root_;
  51. }
  52. private:
  53. TreeNode* root_;
  54. int maxlayer;
  55. int numoflastlayer;
  56. };

解法二:bfs based

先使用bfs将所有子节点数为0和1的节点存入队列,然后维护这个队列,对头节点是插入新节点的节点,若对头节点只有右子树为NULL,那么插入后将其pop,并将其两个子节点指针压入队列。

  1. class CBTInserter{
  2. public:
  3. TreeNode* root_;
  4. queue<TreeNode*> nodes_0_1;
  5. CBTInserter(TreeNode* root){
  6. root_ = root;
  7. queue<TreeNode*> que;
  8. que.push(root);
  9. while(!que.empty()){
  10. TreeNode* now = que.front();
  11. que.pop();
  12. if(now->left == NULL)
  13. nodes_0_1.push(now);
  14. else if(now->right == NULL)
  15. nodes_0_1.push(now);
  16. else{
  17. que.push(now->left);
  18. que.push(now->right);
  19. }
  20. }
  21. }
  22. int insert(int v){
  23. TreeNode* root = nodes_0_1.front();
  24. if(root->left!=NULL){
  25. root->right = new TreeNode(v);
  26. nodes_0_1.pop();
  27. nodes_0_1.push(root->left);
  28. nodes_0_1.push(root->right);
  29. }
  30. else
  31. root->left = new TreeNode(v);
  32. return root->val;
  33. }
  34. TreeNode* get_root(){
  35. return root_;
  36. }
  37. };

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. mongo14-----group,aggregate,mapReduce

    group,aggregate,mapReduce 分组统计: group() 简单聚合: aggregate() 强大统计: mapReduce() db.collection.group(docu ...

  2. YTU 2896: J--Zipper

    2896: J--Zipper 时间限制: 1 Sec  内存限制: 128 MB 提交: 29  解决: 15 题目描述 Given three strings, you are to determ ...

  3. 关于追踪qemu 源码函数路径的一个方法

    这阵子一直在研究qemu 磁盘io路径的源码,发现直接看代码是意见非常低效率的事情,qemu是一个比较庞大的家伙(源码部分大概154MB,完全由C语言来完成),整个结构也都非常地复杂,所以从代码上研究 ...

  4. mac系统下安装mysql步骤

    1.下载mysql-5.7.13-osx10.11-x86_64.dmg安装包,并点击dmg安装包进行安装 2.安装完成后弹出如以下提示信息: 2016-06-23T01:14:48.649253Z ...

  5. 协议森林02 小喇叭开始广播 (以太网与WiFi协议)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁任何形式转载. “小喇叭开始广播啦”,如果你知道这个,你一定是老一辈的人.“小喇叭”是五十年代到八十年代的儿童广播 ...

  6. gcc编译系统

    一. C语言编译过程 C语言的编译过程可分为四个阶段: 1.预处理(Preprocessing) 对源程序中的伪指令(即以#开头的指令)和特殊符号进行处理的过程. 伪指令包括:1)宏定义指令: 2)条 ...

  7. 杂项-Java:JBoss

    ylbtech-杂项-Java:JBoss 是一个基于J2EE的开放源代码的应用服务器. JBoss代码遵循LGPL许可,可以在任何商业应用中免费使用.JBoss是一个管理EJB的容器和服务器,支持E ...

  8. Linux 常用命令六 cp和mv

    一.cp命令 Linux中的复制命令. 复制文件: wang@wang:~/workpalce/python$ tree . ├── .txt ├── dir └── module directori ...

  9. 洛谷P4344 [SHOI2015]脑洞治疗仪(珂朵莉树)

    传送门 看到区间推倒……推平就想到珂朵莉树 挖脑洞直接assign,填坑先数一遍再assign再暴力填,数数的话暴力数 //minamoto #include<iostream> #inc ...

  10. app 后台程序设计

    限制客户端一分钟之内访问接口的次数 1.设备的唯一标识获取这个实际上IOS7后会存在问题,权限已经收回了,android可以2.唯一标识可以通过生成一个token区分3.每分钟的频率可以这样设置 ke ...