LeetCode 919. Complete Binary Tree Inserter
原题链接在这里:https://leetcode.com/problems/complete-binary-tree-inserter/
题目:
A 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 noderoot;CBTInserter.insert(int v)will insert aTreeNodeinto the tree with valuenode.val = vso 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.
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:
- The initial given tree is complete and contains between
1and1000nodes. CBTInserter.insertis called at most10000times per test case.- Every value of a given or inserted node is between
0and5000.
题解:
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的更多相关文章
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- [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
https://leetcode.com/problems/complete-binary-tree-inserter/ 设计一个CBTInserter,使用给定完全二叉树初始化.三个功能; CBTI ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- SpringBoot2.x+Redis+nginx实现session共享和负载均衡
1.创建SpringBoot项目添加依赖 <dependency> <groupId>org.springframework.session</groupId> & ...
- 未安装发布所需的web发布扩展
解决方案:需要安装web deploy 下载网站:https://www.iis.net/downloads/microsoft/web-deploy 假如还是打不开的话,估计时打开方式错误了, 要用 ...
- C#设计模式之11:命令模式
C#设计模式之11:命令模式 命令模式 命令模式用来解决一些复杂业务逻辑的时候会很有用,比如,你的一个方法中到处充斥着if else 这种结构的时候,用命令模式来解决这种问题就会让事情变得简单很多. ...
- 常用正则表达式和一些demo
一.校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ ...
- 【ElasticSearch】查询优化
一.背景 每周统计接口耗时,发现耗时较长的前几个接口tp5个9都超过了1000ms. 经过分析慢查询的原因是ES查询耗时太长导致的 二.设计方案 1.问题定位 查询功能使用不当导致慢查询 索引设计存在 ...
- ES6数组方法总结
关于数组中forEach() .map().filter().reduce().some().every()的总结 1. forEach() let array = [1,2,3,4]; array. ...
- Vue项目开发相关问题总结
Vue项目开发相关问题总结 一.创建一个项目(两种方式) 1.通过CLI命令行创建,具体步骤如下: (1)Node 版本要求 Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11 ...
- JQuey中ready()的4种写法
在jQuery中,对于ready()方法,共有4种写法: (1)写法一: $(document).ready(functin(){ //代码部分 }) 分析:这种代码形式是最常见的,其中$(docum ...
- python day 20: 线程池与协程,多进程TCP服务器
目录 python day 20: 线程池与协程 2. 线程 3. 进程 4. 协程:gevent模块,又叫微线程 5. 扩展 6. 自定义线程池 7. 实现多进程TCP服务器 8. 实现多线程TCP ...
- 数据分析 之 NumPy
目录 简单了解数据分析 Python数据分析三剑客(Numpy,Pandas,Matplotlib) 简单使用np.array() 使用np的routines函数创建数组 ndarray N维数组对象 ...