199. Binary Tree Right Side View
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,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
代码如下:
/**
* 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<Integer> rightSideView(TreeNode root) {
boolean flag=true;
List<Integer> list=new ArrayList<>();
ArrayList<TreeNode> res=new ArrayList<TreeNode>();
Map<Integer,Integer> map=new HashMap<>(); if(root==null)
return list; res=LevelTraverse(root);
list.add(root.val);
map.put(0,0); while(flag)
{
while(root.right!=null)
{
root=root.right;
list.add(root.val);
map.put(res.indexOf(root),res.indexOf(root)); }
if(root.left!=null)
{
root=root.left;
list.add(root.val);
map.put(res.indexOf(root),res.indexOf(root));
}
else{
if(res.indexOf(root)-1<0)
flag=false;
else {
root=res.get(res.indexOf(root)-1);
if(map.containsKey(res.indexOf(root)))
break;
}
}
}
return list;
} public ArrayList<TreeNode> LevelTraverse(TreeNode root)//树的水平遍历
{
ArrayList<TreeNode> list=new ArrayList<TreeNode>();
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(queue.size()>0)
{
TreeNode a=(TreeNode)queue.peek();
queue.remove();
list.add(a);
if(a.left!=null)
queue.add(a.left);
if(a.right!=null)
queue.add(a.right);
} return list;
}
}
199. Binary Tree Right Side View的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】199. Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- [leetcode]199. Binary Tree Right Side View二叉树右侧视角
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
随机推荐
- EasyMock的原理及使用方法
就不费劲转过来了https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/这上面介绍的比较全.
- HTTP协议的chunked编码
一般情况HTTP的Header包含Content-Length域来指明报文体的长度.如: 有时候服务生成HTTP回应是无法确定消息大小的,比如大文件的下载,或者后台需要复杂的逻辑才能全部处理页面的请求 ...
- function format_number(srcNumber, n) {
function format_number(srcNumber, n) {var dstNumber = parseFloat(srcNumber);if (isNaN(dstNumber)) {r ...
- C- printf的使用
ASC C之后引入的一个特性是,相邻的字符可以被自动连接 /* printf.cc * 2014/09/02 update */ #include <iostream> using nam ...
- Problem C HDU 5224
Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows t ...
- Visual studio 2013的安装和单元测试
vs2013安装过程: 简单的单元测试: 1.创建新的c#类库 2.创建单元测试 3.测试结果 单元测试结束
- 新版的tomcat里面get请求通过http协议的时候似乎默认是UTF-8的编码了吗?
不在servler.xml的connector中添加URICoding=“UTF-8”,使用默认值一样没有乱码,而添加URICoding=“iso-8859-1”就是乱码了. POST请求还是用iso ...
- 2013年8月份第4周51Aspx源码发布详情
迷你桌面闹钟源码 2013-8-27 [VS2010]功能介绍:实现了定时闹钟的功能,可以设置闹钟最前端显示.感兴趣的可以下载学习. BR个人博客系统(课程设计)源码 2013-8-27 [VS2 ...
- Connection to http://www.google.com:80 refused
使用SDK Manager更新时出现问题 Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.x ...
- 利用strut2标签自动生成form前端验证代码
利用strut2标签自动生成form前端验证代码,使用到的技术有1.struts2标签,如<s:form> <s:textfieled>2.struts2读取*Validati ...