原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/

题目:

complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Write a data structure CBTInserter that is initialized with a complete binary tree and supports the following operations:

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

Example 1:

Input: inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]]
Output: [null,1,[1,2]]

Example 2:

Input: inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]]
Output: [null,3,4,[1,2,3,4,5,6,7,8]]

Note:

  1. The initial given tree is complete and contains between 1 and 1000 nodes.
  2. CBTInserter.insert is called at most 10000 times per test case.
  3. Every value of a given or inserted node is between 0 and 5000.

题解:

Do level order traversal on the tree. Keep the nodes that doesn't have both left and right child in the queue.

Pop the first node in the queue and mark it as current.

When inserting, check current node's left, if null, insert it as left child. Or check current node's right, if null, insert it as right child. If both not null, then pop another node from queue and insert it as left child.

Time Complexity: CBTInserter, O(n). insert, O(1). get_root, O(1).

Space: O(n). queue space.

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class CBTInserter {
TreeNode root;
TreeNode cur;
LinkedList<TreeNode> que;
public CBTInserter(TreeNode root) {
this.root = root;
que = new LinkedList<TreeNode>();
que.add(root);
while(que.peek().left != null && que.peek().right != null){
TreeNode top = que.poll();
que.add(top.left);
que.add(top.right);
} cur = que.poll();
if(cur.left != null){
que.add(cur.left);
}
} public int insert(int v) {
TreeNode newNode = new TreeNode(v);
que.add(newNode); if(cur.left == null){
cur.left = newNode;
}else if(cur.right == null){
cur.right = newNode;
}else{
cur = que.poll();
cur.left = newNode;
} return cur.val;
} public TreeNode get_root() {
return this.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();
*/

类似Check Completeness of a Binary Tree.

最新文章

  1. 原生js可爱糖果数字时间特效
  2. IOS开发基础知识--碎片13
  3. php 解析 视频 信息 封面 标题 图片 支持 优酷, 土豆 酷6 56 新浪 qq播客 乐视 乐视
  4. IE和Firefox的Javascript兼容性总结
  5. 在存储过程中执行3种oracle循环语句
  6. hive 调用java的函数和科学记数法转换
  7. NYOJ-85 有趣的数 AC 分类: NYOJ 2014-01-17 21:42 240人阅读 评论(0) 收藏
  8. 使用IO流创建文件并写入数据
  9. Linux下的vi编辑命令中查找&#183;替换详解
  10. SQL GROUP BY GROUPING SETS,ROLLUP,CUBE(需求举例)
  11. Line Painting
  12. aspx.cs上传文件
  13. Angular JS的正确打开姿势——简单实用(下)
  14. 关于webp图片格式初探
  15. session_id() , session_start(), $_SESSION[&quot;userId&quot;], header(&quot;Location:homeLogin.php&quot;); exit 如果没有登录, 就回登录页
  16. 关于Nginx
  17. jQuery插件EasyDrag轻松实现JS拖动的效果
  18. win10 QQ远程协助部分界面点不了
  19. 快速用梯度下降法实现一个Logistic Regression 分类器
  20. 使HTML页面上获取到的文本保留空格和换行符等格式

热门文章

  1. 嵌入式02 STM32 实验08 外部中断
  2. 第1课,python输出,输入,变量,运算
  3. 刨根究底字符编码之十六——Windows记事本的诡异怪事:微软为什么跟联通有仇?(没有BOM,所以被误判为UTF8。“联通”两个汉字的GB内码,其第一第二个字节的起始部分分别是“110”和“10”,,第三第四个字节也分别是“110”和“10”)
  4. 微信小程序文档
  5. C语言--线性表
  6. C# vb .net实现颜色替换效果滤镜
  7. docker save load export import的区别
  8. ASP.NET SignalR 系列(四)之指定对象推送
  9. oracle 数据库导入导出语句
  10. springboot使用RestHighLevelClient7简单操作ElasticSearch7增删查改/索引创建