LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II
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,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
package leetcode.easy; /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class BinaryTreeLevelOrderTraversalII {
// DFS solution:
public java.util.List<java.util.List<Integer>> levelOrderBottom1(TreeNode root) {
java.util.List<java.util.List<Integer>> wrapList = new java.util.LinkedList<java.util.List<Integer>>();
levelMaker(wrapList, root, 0);
return wrapList;
} private static void levelMaker(java.util.List<java.util.List<Integer>> list, TreeNode root, int level) {
if (root == null) {
return;
}
if (level >= list.size()) {
list.add(0, new java.util.LinkedList<Integer>());
}
levelMaker(list, root.left, level + 1);
levelMaker(list, root.right, level + 1);
list.get(list.size() - level - 1).add(root.val);
} // BFS solution:
public java.util.List<java.util.List<Integer>> levelOrderBottom2(TreeNode root) {
java.util.Queue<TreeNode> queue = new java.util.LinkedList<TreeNode>();
java.util.List<java.util.List<Integer>> wrapList = new java.util.LinkedList<java.util.List<Integer>>();
if (root == null) {
return wrapList;
}
queue.offer(root);
while (!queue.isEmpty()) {
int levelNum = queue.size();
java.util.List<Integer> subList = new java.util.LinkedList<Integer>();
for (int i = 0; i < levelNum; i++) {
if (queue.peek().left != null) {
queue.offer(queue.peek().left);
}
if (queue.peek().right != null) {
queue.offer(queue.peek().right);
}
subList.add(queue.poll().val);
}
wrapList.add(0, subList);
}
return wrapList;
} @org.junit.Test
public void test() {
TreeNode tn11 = new TreeNode(3);
TreeNode tn21 = new TreeNode(9);
TreeNode tn22 = new TreeNode(20);
TreeNode tn33 = new TreeNode(15);
TreeNode tn34 = new TreeNode(7);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(levelOrderBottom1(tn11));
System.out.println(levelOrderBottom2(tn11));
}
}
LeetCode_107. Binary Tree Level Order Traversal II的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【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 ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- Binary Tree Level Order Traversal II 解题思路
思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...
随机推荐
- URI和URL、REST
URI和URL URI(Uniform Resource Identifier ) 是一个紧凑的字符串用来标示抽象或物理资源.可以分为URL,URN或同时具备locators 和names特性的一个东 ...
- Mac+appium+iOS 环境搭建
Mac+appium+iOS 环境搭建,需要用到的信息如下,参考搭建环境. 1.安装brew,安装介绍:https://jingyan.baidu.com/article/fec7a1e5ec3034 ...
- Django Forms的错误提示
1.error_messages={} 首先,在构建form表单时,可以用"error_messages={}"自定义错误信息,例如: # form.py 1 from djang ...
- [bzoj 1471] 不相交路径 (容斥原理)
题目描述 给出一个N(n<=150)N(n<=150)N(n<=150)个结点的有向无环简单图.给出444个不同的点aaa,bbb,ccc,ddd,定义不相交路径为两条路径,两条路径 ...
- 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor
33.[源码]-AOP原理-获取拦截器链-MethodInterceptor
- SublimeText 括号插件 Bracket Highlighter高亮设置
1. ctrl + shift + p,打开命令面板,输入install,在菜单中选择Package Control:Install Package如图 2. 步骤1后弹出的命令输入框中 输入:Bra ...
- E:nth-last-child(n)
E:nth-last-child(n) 语法: E:nth-last-child(n) { sRules } 说明: 匹配父元素的倒数第n个子元素E,假设该子元素不是E,则选择符无效.大理石平台维修 ...
- URL中的String参数问题
测试一个查询数据的接口,类似这样的URL:.../search?type=Astring,在浏览器中输入URL获取到的数据为空,但通过其它方式确认数据库中确实已有数据,怀疑是接口实现问题.找接口实现的 ...
- spring boot + mybaits 处理枚举类 enum
枚举: //实现层调用 orderMapper.getOrder(OrderStatus.DISCOUNT); sql打印: 实际sql: select * from order where orde ...
- Codechef July Challenge 2019 Division 1题解
题面 \(CIRMERGE\) 破环成链搞个裸的区间\(dp\)就行了 //quming #include<bits/stdc++.h> #define R register #defin ...