Easy-题目33:102. Binary Tree Level Order Traversal
来源:互联网 发布:java体系结构图 编辑:程序博客网 时间:2024/05/22 23:48
题目原文:
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%
0 0
- Easy-题目33:102. Binary Tree Level Order Traversal
- Easy-题目27:107.Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal (easy)
- 102. Binary Tree Level Order Traversal [easy] (Python)
- Leetcode 102. Binary Tree Level Order Traversal (Easy) (cpp)
- LeetCode解题报告 102. Binary Tree Level Order Traversal [easy]
- 107. Binary Tree Level Order Traversal II [easy] (Python)
- Leetcode 107. Binary Tree Level Order Traversal II (Easy) (cpp)
- Easy 102题 Binary Tree Level Order Traversal
- LeetCode-Easy刷题(22)Binary Tree Level Order Traversal II
- LeetCode 102. Binary Tree Level Order Traversal
- [LeetCode]102.Binary Tree Level Order Traversal
- [Leetcode] 102. Binary Tree Level Order Traversal
- LeetCode --- 102. Binary Tree Level Order Traversal
- [leetcode] 102.Binary Tree Level Order Traversal
- 102.Binary Tree Level Order Traversal
- 102. Binary Tree Level Order Traversal
- 【leetcode】102. Binary Tree Level Order Traversal
- libubus 数据结构介绍
- 在Ubuntu系统下装Win7并引导双系统
- 《JavaScript学习笔记八》:数字时钟的实现
- Codeforces Round #353 (Div. 2) E
- 由于drawable目录下新建文件夹导致的AndroidStudio编译失败解决思路!
- Easy-题目33:102. Binary Tree Level Order Traversal
- 第十三周项目1:阅读、修改和运行关于交通工具类的程序(1)
- TaoCloud SSAN+oVirt系虚拟化解决方案
- linux文件系统启动流程 ---笔记整理
- MYSQL的创建数据表及测试数据类型
- 一定要战斗下去
- HDU 5713 & 2016"百度之星" 复赛(Astar Round3)1002 k个联通块
- 《JavaScript学习笔记九》:延时框的实现
- poj 1511 spfa入门