Easy-题目33:102. Binary Tree Level Order Traversal

原创 2016年05月30日 20:19:46

题目原文:
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
题目大意:
按层次遍历二叉树。
题目分析:
与前面的Easy第27题类似,只是不用翻转最后的结果数组。
源码:(language:java)

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        Queue<Integer> levelqueue=new LinkedList<Integer>();

        queue.add(root);
        levelqueue.add(1);
        List<List<Integer>> result=new ArrayList();
        List<Integer> temp=new ArrayList<Integer>();
        if(root==null)
            return result;
        while(!queue.isEmpty()) {
            TreeNode current=queue.remove();
            int curLevel=levelqueue.remove();
            if(curLevel==result.size())
                temp.add(current.val);
            else {
                result.add(temp);   
                temp=new ArrayList<Integer>();
                temp.add(current.val);  
            }
            if(current.left!=null) {
                queue.add(current.left);
                levelqueue.add(curLevel+1);
            }
            if(current.right!=null) {
                queue.add(current.right);
                levelqueue.add(curLevel+1);
            }
        }
        result.add(temp);
        result.remove(0);
        return result;        
    }
}

成绩:
3ms,beats 14.37%,众数3ms,46.37%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

102. Binary Tree Level Order Traversal [easy] (Python)

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

LeetCode(102)Binary Tree Level Order Traversal

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

LeetCode102 Binary Tree Level Order Traversal Java

题目: Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to r...

Binary Tree Level Order Traversal I II(102、107)

Binary Tree Level Order Traversal
  • zzIymt
  • zzIymt
  • 2016年02月21日 15:09
  • 488

Leetcode 102 Binary Tree Level Order Traversal

题目:https://leetcode.com/problems/binary-tree-level-order-traversal/ 九章算法答案:http://www.jiuzhang.com/...

LeetCode 102 Binary Tree Level Order Traversal

题目描述Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to ri...

[102] Binary Tree Level Order Traversal

给定一个二叉树,返回一个二维List,每个list中保存二叉树中对应层的节点。...
  • camlot_
  • camlot_
  • 2016年06月30日 14:32
  • 107

leetcode-102-Binary Tree Level Order Traversal

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

第三周 leetcode 102. Binary Tree Level Order Traversal(Medium)

题目描述: Given a binary tree, return the level order traversal of its nodes’ values.
  • renzhff
  • renzhff
  • 2017年07月14日 15:02
  • 54

LeetCode - 102. Binary Tree Level Order Traversal

Binary Tree Level Order Traversal使用queue来实现,level order traversal也相当是一种bfs的算法。首先将root节点加入queue中,接下来当...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目33:102. Binary Tree Level Order Traversal
举报原因:
原因补充:

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