原题链接在这里: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.

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. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  3. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  4. leetcode_919. Complete Binary Tree Inserter

    https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...

  5. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. PAT1110:Complete Binary Tree

    1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

随机推荐

  1. OpenLayers加载谷歌地球离线瓦片地图

    本文使用OpenLayers最新版本V5.3.0演示:如何使用OpenLayer加载谷歌地球离线瓦片地图.OpenLayers 5.3.0下载地址为:https://github.com/openla ...

  2. 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76

    package y2019.Algorithm.str.hard; import java.util.Stack; /** * @ProjectName: cutter-point * @Packag ...

  3. [cf 1239 B] The World Is Just a Programming Task (Hard Version)

    题意: 给你一个长度为n的括号序列,你可以交换其中的两个元素,需要使该序列的n个循环移位中合法的括号序列个数尽量多. 输出最大的答案以及交换哪两个元素能够取到这个答案. $n\leq 3\times ...

  4. 中控考勤机使用 zkemkeeper SDK订阅考勤数据事件失效解决方式

    问题 前同事编写的对中控考勤机数据集成项目当中,打卡数据不能实时进行上传到平台当中,一直靠定时全量上传来同步数据. 阅读代码后,发现代码中有实时上传数据的逻辑,但是运行一段时间后,中控zkemkeep ...

  5. 2019 汽车之家java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.汽车之家等公司offer,岗位是Java后端开发,最终选择去了汽车之家. 面试了很多家公司,感觉大部分公司考察的点 ...

  6. 你需要知道的 5 个 Linux 新手会犯的失误

    Linux 爱好者们分享了他们犯下的一些最大错误. 终身学习是明智的 —— 它可以让你的思维敏捷,让你在就业市场上更具竞争力.但是有些技能比其他技能更难学,尤其是那些小菜鸟错误,当你尝试修复它们时可能 ...

  7. ES6--Promise讲解

    相信凡是写过javascript的童鞋也一定都写过回调方法(callback),简单说回调方法就是将一个方法func2作为参数传入另一个方法func1中,当func1执行到某一步或者满足某种条件的时候 ...

  8. mysql 根据日期进行查询数据,没有数据也要显示空

    写这篇博客主要是记录自己在对订单进行按日期查询时使用的一种查询的方法,这里的orders是订单表,你也可以改成别的什么表对于最终数据不会造成影响,除非你那个表的数据只有几条那样就会出现查不到日期的情况 ...

  9. Postgresql 常用操作

    表所有者一括变更 select 'alter table public.'|| tablename ||' owner to appuser;' from pg_tables where tableo ...

  10. java-检查IP和端口在超时时间内是否可以连接

    检查IP和端口在超时时间内是否可以连接 /** * 检查IP和端口是否可以连接 * @param ip * @param port * @return */ private static boolea ...