[LeetCode 199] Binary Tree Right Side View
2015-08-21 13:36
483 查看
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
You should return
Solution:
1. use queue to do level iteration.
For example:
Given the following binary tree,
1 <--- / \ 2 3 <--- \ \ 5 4 <---
You should return
[1, 3, 4].
Solution:
1. use queue to do level iteration.
public List<Integer> rightSideView(TreeNode root) { List<Integer> result = new LinkedList<>(); if(root == null) return result; Queue<TreeNode> levelNodes = new LinkedList<>(); levelNodes.offer(root); while(levelNodes.size()>0){ TreeNode n2 = levelNodes.peek(); int len = levelNodes.size(); for(int i=0;i<len;i++){ TreeNode n1 = levelNodes.poll(); if(n1.right!=null) levelNodes.offer(n1.right); if(n1.left!=null) levelNodes.offer(n1.left); } result.add(n2.val); } return result; }
相关文章推荐
- ios各种动画效果
- poj 3370 Halloween treats (剩余类,抽屉原理)
- HTML5之SVG详解(一) 时间 2012-12-21 19:49:00 博客园-原创精华区 原文 http://www.cnblogs.com/hupeng/archive/2012/12/2
- ImageLoader介绍
- Android比较好的10个开源框架
- flumeng收集日志并存储到HDFS
- 域名解析-CNAME
- c盘没有新建修改权限的,执行下面命令
- [HDU 1530] Maximum Clique 最大团
- 【原】让代码轻盈地飞舞——IDEA13最实用的功能,全面提升开发效率(桌面java项目开发实战)持续更新中
- paramics小车车辆属性相关API
- Linux系统启动
- PAT 1054. The Dominant Color (20)
- 欢迎使用CSDN-markdown编辑器
- A轮融资
- 【免费算法课】硅谷工程师直播面试技巧!
- 【ODPS】MapReduce基础
- codeforces 16C C. Monitor(二分)
- 未能加载文件或程序集“WcfService”或它的某一个依赖项。试图加载格式不正确的程序。
- CSS float 父元素高度自适应