leetcode Binary Tree Level Order Traversal
2015-05-29 21:57
239 查看
题目链接链接
思想:广度优先算法
遇到的错误
1空链表不等于null
2入队的东西取出来要记得剔除
思想:广度优先算法
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { TreeNode endLineFlag=new TreeNode(0); List<List<Integer>> myArray= new LinkedList<List<Integer>> (); LinkedList<Integer> workContiner=new LinkedList<Integer>(); ArrayDeque<TreeNode> queue=new ArrayDeque<TreeNode>(); if(root==null) { return myArray; } queue.addLast(root); queue.addLast(endLineFlag); TreeNode current=null; while(queue.size()>1) { current=queue.remove(); if(current==endLineFlag) { myArray.add(workContiner); workContiner=new LinkedList<Integer>(); queue.addLast(endLineFlag); continue; } workContiner.add(current.val); if(current.left!=null) { queue.add(current.left); } if(current.right!=null) { queue.add(current.right); } } myArray.add(workContiner); return myArray; } }
遇到的错误
1空链表不等于null
2入队的东西取出来要记得剔除
相关文章推荐
- MySQL 数据库性能优化之表结构
- Spring.net--AOP
- Emacs中使用R
- 利用51单片机制作的电子时钟
- 命令中的特殊字符
- 话说物联网操作系统
- 第13周-多态性--项目0-课后实践·阅读程序2.
- 利用51单片机制作的电子时钟
- windows配置redmine
- 【架构师之旅】ITOO框架总结
- SharePoint 部署解决方案Feature ID冲突
- 零基础学习hadoop到上手工作线路指导(初级篇)
- 第十三周阅读项目(纯虚构函数)
- MySQL 数据库性能优化之缓存参数优化
- SharePoint 部署解决方案Feature ID冲突
- setTimeout的异步
- MYSQL 日期函数大全
- Word Amalgamation(hdoj1113)
- http://www.zhihuishi.com/class/1
- Git学习笔记之Git常用命令