[LeetCode]Binary Tree Level Order Traversal II,解题报告

原创 2013年12月03日 13:42:03

前言

中午容易犯困,做两道ACM有助于活跃脑细胞,大笑,汗了,csdn还可以添加qq标清,拜托支持一下markdown语法吧


题目

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


思路

题目不难,说是从底向上按层遍历二叉树,其实偷个懒,从上向下挨层遍历之后,然后对ArrayList里的元素来个逆序就OK了

从上向下挨层遍历二叉树,参考链接:java集合LinkeLlist模拟队列

ArrayList元素逆序,可以自己写个翻转的方法(双指针O(n)),也可以直接调用Collections类里的静态方法reverse


AC代码

/**
 * 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>> list = new ArrayList<ArrayList<Integer>>();
        LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode p;
        
        if (root == null) {
            return list;
        }
        
        queue.addLast(root);
        
        while (! queue.isEmpty()) {
            ArrayList<Integer> sublist = new ArrayList<Integer>();
            LinkedList<TreeNode> subqueue = new LinkedList<TreeNode>();
            
            while (! queue.isEmpty()) {
                p = queue.removeFirst();
                sublist.add(p.val);
                
                if (p.left != null) {
                    subqueue.addLast(p.left);
                }
                
                if (p.right != null) {
                    subqueue.addLast(p.right);
                }
            }
            
            list.add(sublist);
            queue.addAll(subqueue);
        }
        
        Collections.reverse(list);
        return list;
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【LeetCode】Binary Tree Level Order Traversal II 解题报告

Binary Tree Level Order Traversal II [LeetCode] Given a binary tree, return the bottom-up level orde...

[leetcode]107. Binary Tree Level Order Traversal II@Java解题报告

https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/ Given a b...

[leetcode] 107. Binary Tree Level Order Traversal II 解题报告

题目链接:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, retu...

leetCode解题报告之Binary Tree Level Order Traversal II,I(二叉树层次遍历)

题目: Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order tra...

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

[Leetcode] 103. Binary Tree Zigzag Level Order Traversal 解题报告

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, fro...

Leetcode 103. Binary Tree Zigzag Level Order Traversal 二叉树Zigzag遍历 解题报告

1 解题思想首先这道题和 Leetcode 102. Binary Tree Level Order Traversal 二叉树按层遍历 解题报告 很接近,Zigzag同样是按层遍历,但是区别是对...
  • MebiuW
  • MebiuW
  • 2016年09月09日 12:26
  • 782

【LeetCode】102. Binary Tree Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51477967Subject 出处:https://leetcode.com/pro...

[leetcode]102. Binary Tree Level Order Traversal@Java解题报告

https://leetcode.com/problems/binary-tree-level-order-traversal/description/ Given a bina...

【LeetCode】Binary Tree Level Order Traversal 解题报告

Binary Tree Level Order Traversal [LeetCode] Given a binary tree, return the level order traversal o...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode]Binary Tree Level Order Traversal II,解题报告
举报原因:
原因补充:

(最多只允许输入30个字)