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 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]
] 题解:
这道题跟前面一道是一样的。。
只是存到res的结果顺序不一样罢了。
之前那个就是循序的存
这道题就是每得到一个行结果就存在res的0位置,这样自然就倒序了。 代码如下:
1 public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
2 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
3 if(root == null)
4 return res;
5
6 LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
7 queue.add(root);
8
9 int curLevCnt = 1;
int nextLevCnt = 0;
ArrayList<Integer> levelres = new ArrayList<Integer>();
while(!queue.isEmpty()){
TreeNode cur = queue.poll();
curLevCnt--;
levelres.add(cur.val);
if(cur.left != null){
queue.add(cur.left);
nextLevCnt++;
}
if(cur.right != null){
queue.add(cur.right);
nextLevCnt++;
}
if(curLevCnt == 0){
curLevCnt = nextLevCnt;
nextLevCnt = 0;
res.add(0,levelres); //insert one by one from the beginning
levelres = new ArrayList<Integer>();
}
}
return res;
}
Binary Tree Level Order Traversal II leetcode java的更多相关文章
- 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]
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)
这是悦乐书的第165次更新,第167篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107).给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 开发人员如何正确对待BUG?
1.前端开发与后端开发 出了问题,最重要的是先找到方法迅速解决,而不是去互相指责.前端存在这样的思维模式,后端也存在这样的思维模式,这种思维模式不太好.出了问题,最好先检查一下自己,反省是不是自己这 ...
- 模板 树上求LCA 倍增和树链剖分
//233 模板 LCA void dfs(int x,int f){ for(int i=0;i<E[x].size();i++){ int v = E[x][i]; if(v==f)cont ...
- spring data redis的配置类RedisConfig
package com.tz.config; import org.springframework.context.annotation.Bean; import org.springframewor ...
- RabbitMQ消息交换模式简介
RabbitMQ是AMQP的一个典型实现,它消息发布者的消息发布到Exchange上,同时需要制定routingkey,可以通过指定交换机的不同模式实现不同的行为. RabbitMQ提供了四种Exch ...
- STM32 Timer : Auto-reload register register
Auto-reload register (TIMx_ARR) The auto-reload register is preloaded. Writing to or reading from th ...
- 【leetcode】 Permutation Sequence
问题: 对于给定序列1...n,permutations共同拥有 n!个,那么随意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小開始直接到k, ...
- GetBuiltProjectOutputRecursive error running Xamarin Forms iOS on Visual Studio
Seems like I get this weird problem while running Xamarin.iOS on Visual studio. This happened after ...
- 一个例子来看C#泛型是如何登场的
有这样一个有关汽车的类. public class Car { public int ID { get; set; } public string Make { get; set; } } 现在,在客 ...
- rTorrent + ruTorrent 安装和配置
原文地址:http://wangyan.org/blog/rtorrent-and-rutorrent-tutorial.html rTorrent 是一款非常简洁优秀的BT客户端,它完全基于文本并可 ...
- Android开发 AIDL使用自定义对象作参数或返回值
http://www.pocketdigi.com/20121129/952.html 默认,AIDL支持对象作参数,但需要该对象实现Parcelable接口,且aidl文件应该是该类在同一包下,需要 ...