Leetcode: Binary Tree Level Order Transversal 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,#,#,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补齐空格

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public List<List<Integer>> levelOrderBottom(TreeNode root) {
12         List<List<Integer>> res = new ArrayList<List<Integer>>();
13         if (root == null) return res;
14         Queue<TreeNode> queue = new LinkedList<TreeNode>();
15         queue.offer(root);
16         while (!queue.isEmpty()) {
17             List<Integer> item = new ArrayList<Integer>();
18             int size = queue.size();
19             for (int i=0; i<size; i++) {
20                 TreeNode cur = queue.poll();
21                 item.add(cur.val);
22                 if (cur.left != null) {
23                     queue.add(cur.left);
24                 }
25                 if (cur.right != null) {
26                     queue.add(cur.right);
27                 }
28             }
29             res.add(0, new ArrayList<Integer>(item));
30         }
31         return res;
32     }
33 }

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

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
12         ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>> ();
13         if (root == null) return lists;
14         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
15         queue.add(root);
16         int ParentNumInQ = 1;
17         int ChildNumInQ = 0;
18         ArrayList<Integer> list = new ArrayList<Integer>();
19         while (!queue.isEmpty()) {
20             TreeNode cur = queue.poll();
21             list.add(cur.val);
22             ParentNumInQ--;
23             if (cur.left != null) {
24                 queue.add(cur.left);
25                 ChildNumInQ++;
26             }
27             if (cur.right != null) {
28                 queue.add(cur.right);
29                 ChildNumInQ++;
30             }
31             if (ParentNumInQ == 0) {
32                 ParentNumInQ = ChildNumInQ;
33                 ChildNumInQ = 0;
34                 lists.add(list);
35                 list = new ArrayList<Integer>();
36             }
37         }
38         Collections.reverse(lists);
39         return lists;
40     }
41 }

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

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

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

Input:{}Output:nullExpected:[]

 

DFS 做法:

 1 public class Solution {
 2         public List<List<Integer>> levelOrderBottom(TreeNode root) {
 3             List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
 4             levelMaker(wrapList, root, 0);
 5             return wrapList;
 6         }
 7         
 8         public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
 9             if(root == null) return;
10             if(level >= list.size()) {
11                 list.add(0, new LinkedList<Integer>());
12             }
13             levelMaker(list, root.left, level+1);
14             levelMaker(list, root.right, level+1);
15             list.get(list.size()-level-1).add(root.val);
16         }
17     }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/3972648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值