[Leetcode] binary tree 右视问题
2015-08-17 10:10
507 查看
[1] Populating Next Right Pointers in Each Node
[2] Populating Next Right Pointers in Each Node II
[3] Binary Tree Right Side View
一、参考我的另一篇博客
二、同一
三、使用层次遍历方式,但是要记录每一层的最后一个node
[2] Populating Next Right Pointers in Each Node II
[3] Binary Tree Right Side View
一、参考我的另一篇博客
二、同一
三、使用层次遍历方式,但是要记录每一层的最后一个node
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*; public class Solution { public List<Integer> rightSideView(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); List<Integer> res = new LinkedList<Integer>(); if(root==null) return res; queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); TreeNode current = null; for(int i=0;i<size;i++){ current = queue.poll(); if(current.left!=null) queue.offer(current.left); if(current.right!=null) queue.offer(current.right); } res.add(current.val); } return res; } }
相关文章推荐
- 调试UnhandledExceptionFilter
- 笔记一
- 黑马vivo的逆袭之路
- -fembed-bitcode is not supported on versions of iOS prior to 6.0
- GitLab Cl Deploy - docker build push
- Mac Tips(持续更新)
- 国际化
- 九、缓冲IO的高级功能
- app后端开发系列文章目录
- Java数据类型的转换:隐式(自动)转换与强制转换
- BZOJ 1601 [Usaco2008 Oct]灌水 MST
- GIC400简介
- 测试博客发表功能
- Chrome浏览器快捷键大全
- 启动tomcat7.0时报错:unable to start within 45 seconds. If the server requires more time, try increasing t
- 10分钟的学习,改变你的程序员生涯
- 选择排序法--java实现
- Java compiler level does not match the version of the instal
- HDU1698--Just a Hook(成段更新)
- 关于一个oc写的命令行版的通讯录程序(代码贴过来无缩进,作为备忘,代码完成度:90%)