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的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  4. 【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, ...

  5. 【刷题-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, ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. centos下cmake安装

    步骤一.安装gcc等必备程序包(已安装则略过此步,用gcc -v检测) yum install -y gcc gcc-c++ make automake 步骤二.安装wget (已安装则略过此步) y ...

  2. C#窗体的加载等待(BackgroundWorker控件)实现

    窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...

  3. c# MVC在WEB.Config中配置MIME

    在IIS中,默认没有添加.json格式的MIME,所有无法读取服务器中的.json格式的文件,返回结果404 方式一:在IIS中手动添加MIME 1.点击MIME进入MIME列表 2.添加MIME 3 ...

  4. C++-模板的声明和实现为何要放在头文件中

    源: http://blog.csdn.net/lqk1985/archive/2008/10/24/3136364.aspx 如何组织编写模板程序 发表日期: 1/21/2003 12:28:58 ...

  5. Fragment 切换问题

    public void switchContent(Fragment fragment) { if(mContent != fragment) { mContent = fragment; mFrag ...

  6. java基础-002

    1.Java虚拟机和“平台无关语言” Java虚拟机是可以执行字节码的虚拟机进程.Java源文件被编译成被Java虚拟机执行的字节码文件. Java被设计成允许应用程序运行在任意的平台,而不需要程序员 ...

  7. iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020)

    看看IIS的网站,惊人的发现default web site是停止状态.印象中没有停止它啊.右键->管理网站->启动.点击启动后居然弹出:“另一个程序正在使用此文件,进程无法访问.(异常来 ...

  8. Java选择结构、循环结构

    1:switch语句(掌握) (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; ...

  9. Stm32_调试出现 Error:Flash Download Failed-"Cortex-M3"

    rror:Flash Download Failed-"Cortex-M3"出现一般有两种情况: 1.SWD模式下,Debug菜单中,Reset菜单选项(Autodetect/HW ...

  10. mp3文件 ID3v2 帧标识的含义

    mp3文件 ID3v2 帧标识的含义 Declared ID3v2 frames The following frames are declared in this draft. 4.20 AENC ...