Binary Tree Level Order Traversal II——LeetCode
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
题目大意为,按照层次逆序输出每一层的节点,很明显用层次遍历即可,之前在Minimum Depth of Binary Tree ——LeetCode这个题目中,也用到了层次遍历,下面先看我写的,也是用upRow,downRow,来记录上一层、下一层的节点数量,upRow--为0时,一层遍历完了。
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<Integer> level = new LinkedList<>();
List<List<Integer>> res = new LinkedList<>();
if (root == null)
return res;
ArrayDeque<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
int upRow = 1, downRow = 0;
while (!queue.isEmpty()) {
TreeNode node = queue.getFirst();
queue.removeFirst();
if (node.left != null) {
queue.add(node.left);
downRow++;
}
if (node.right != null) {
queue.add(node.right);
downRow++;
}
upRow--;
level.add(node.val);
if (upRow == 0) {
res.add(0, level);
level = new LinkedList<>();
upRow = downRow;
downRow=0;
}
}
return res;
}
后来发现一种更巧妙的解,就是利用queue的size(),根本不用记录这两个值。
Talk is cheap>>
public List<List<Integer>> levelOrderBottom2(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> wrapList = new LinkedList<>(); if (root == null) return wrapList;
queue.offer(root);
while (!queue.isEmpty()) {
int levelNum = queue.size();
List<Integer> subList = new LinkedList<>();
//把一层元素全部取出
for (int i = 0; i < levelNum; i++) {
TreeNode node = queue.poll();//取出队列第一个元素
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
subList.add(node.val);
}
wrapList.add(0, subList);
}
return wrapList;
}
Binary Tree Level Order Traversal II——LeetCode的更多相关文章
- Binary Tree Level Order Traversal II [LeetCode]
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Binary Tree Level Order Traversal II --leetcode C++
考察点 广度优先遍历--层次遍历 STL内容器的用法 广度优先遍历的时候,首先应该想到的就是借助于队列.还需要在遍历下一层之前保存当前层节点的数量 代码很简单: class Solution { pu ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
随机推荐
- 获取外网IP地址
public static string GetRealIP(){ string result = String.Empty; result = HttpC ...
- Scene is unreachable due to lack of entry points and does not have an identifier for runtime access
使用Storyboard时出现以下警告: warning: Unsupported Configuration: Scene is unreachable due to lack of entry p ...
- JavaScript--对象-检查一个对象是否是数组
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Linux硬盘命名和安装分区
硬盘命名: 硬盘命名基于文件,一般有如下文件方式: /dev/hda1 /dev/sdb3 具体含义如下: /dev:是所有设备文件存放的目录. hd和sd:他们是区别的前两个字母,代表该分区所在的设 ...
- jquery find选择器在不同浏览器下的差异
初步测试,5000个节点的隐藏. 代码如下: <!doctype html> <html lang="en"> <head> <scrip ...
- asp.net使用Mysql乱码处理
在asp.net与mysql数据库打交道的时候,由于配置的问题,会遇到自己写的方法在读取数据库中数据的时候,英文,数字可以正常通过,但是中文就无法通过,以登录为例(方法略),当输入英文用户名的时候可以 ...
- jquery validate 配合ligerui使用
这样当单击登录按钮时右边会弹出一个小框框,同时显示jquery扩展的下面那段代码错误内容,当添加内容正确后小框框消失,lable换成error也行 $("#form1").vali ...
- php中的双引号和单引号的区别?
1.单引号里面的字符串直接全部转义,原样输出(即:单引号内部的变量不会被执行) 2.双引号里面的变量会被替换(即:变量会执行) 例如:$name = 'hello';echo "the $n ...
- memcache锁
锁的使用,一般情况是针对并发或者我们希望程序(crontab的job)串行处理,我们加锁的办法有很多,像文件锁,数据库锁,或者memcache锁,这里关注一下memcache锁,针对memcache锁 ...
- python种的builtin函数详解-第三篇
exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] eval(e ...