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]
]

第二遍方法:

这道题在groupon面经里面有,有一个follow up 是能不能右对齐输出。那就在29行记录每一行的最大size,然后在输出的时候根据最大size补齐空格

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> item = new ArrayList<Integer>();
int size = queue.size();
for (int i=0; i<size; i++) {
TreeNode cur = queue.poll();
item.add(cur.val);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
res.add(0, new ArrayList<Integer>(item));
}
return res;
}
}

在Binary Tree Level Order Transversal的基础上难度:20,只需要对最后结果做一个倒序就好。格式是Collections.reverse(List<?> list)

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>> ();
if (root == null) return lists;
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
int ParentNumInQ = 1;
int ChildNumInQ = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
list.add(cur.val);
ParentNumInQ--;
if (cur.left != null) {
queue.add(cur.left);
ChildNumInQ++;
}
if (cur.right != null) {
queue.add(cur.right);
ChildNumInQ++;
}
if (ParentNumInQ == 0) {
ParentNumInQ = ChildNumInQ;
ChildNumInQ = 0;
lists.add(list);
list = new ArrayList<Integer>();
}
}
Collections.reverse(lists);
return lists;
}
}

注意38行的写法,Collections.reverse()跟Arrays.sort()函数一样,都是void返回型,然后改变作用在argument上

还有if (root == null) return null;

ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();这样会出错:

Input:{}Output:nullExpected:[]

DFS 做法:

 public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
levelMaker(wrapList, root, 0);
return wrapList;
} public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
if(root == null) return;
if(level >= list.size()) {
list.add(0, new LinkedList<Integer>());
}
levelMaker(list, root.left, level+1);
levelMaker(list, root.right, level+1);
list.get(list.size()-level-1).add(root.val);
}
}

Leetcode: Binary Tree Level Order Transversal II的更多相关文章

  1. [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 ...

  2. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  3. [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 ...

  4. 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 ...

  5. LeetCode - Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  6. Leetcode: Binary Tree Level Order Transversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. LeetCode "Binary Tree Level Order Traversal II" using DFS

    BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...

  8. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  9. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

随机推荐

  1. set数组去重

    new Set const arr = [{name:"wo"},{name:"shi"},{name:"wo"}] console.log ...

  2. Testlink自动执行用例小程序

    记得原来在一个公司时,具体很多原因,testlink上项目中的用例都需要执行形成漂亮的报告,但实际测试中又不需要去执行,所以就必须将用例根据上一次测试报告一个一个手工去贴结果刷用例,几百条用例,几天就 ...

  3. mysql如何使用索引index提升查询效率?

    https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html Indexes are used to find rows with specif ...

  4. MAC - 系统升级导致COCOAPODS失效问题

    使用pod install出现如下错误: macdeMacBook-Pro:QRCodeDemo mac$ pod install -bash: /usr/local/bin/pod: /System ...

  5. mysql if判断

    select if(SUBSTR('06622200556',1,2)='06',0,1) from t_member_product_adb limit 2 输出结果为:0,0

  6. Spark2 Dataset持久化存储级别StorageLevel

    import org.apache.spark.storage.StorageLevel // 数据持久缓存到内存中//data.cache()data.persist() // 设置缓存级别data ...

  7. python3+socket搭建简易服务器

    踩了一上午的坑之后,终于对网络编程有了一点大致的.基本的了解.真的是0基础,之前对socket网络编程一点都不知道.(感觉自己与时代脱轨....) 首先我想对这些美妙的专业术语进行一番搜索: 服务器: ...

  8. 8.21 js

    2018-8-21 20:05:43 2018-8-21 20:56:30 明天再看!!!! 今天空闲多看了书 <百年孤独> <苏东坡传> 打印结果  shanghai js的 ...

  9. 使用pidstat监控资源使用

    linux可以使用pidstat命令监控系统资源,比如监控cup使用如下: pidstat -u 还可以使用 -r(内存) -d(硬盘)

  10. ls 列出文件目录(可以含子目录)及文件的完整路径

    1.列出当前目录的文件.文件夹完整路径    ls -1 |awk '{print i$0}' i=`pwd`'/' 2.列出当前目录及子目录的文件.文件夹完整路径    ls -R |awk '{p ...