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

解题思路:

DFS,带个height即可,JAVA实现如下:

	public List<Integer> rightSideView(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
list.add(root.val);
if (root.right != null)
dfs(list, root.right, 1);
if (root.left != null)
dfs(list, root.left, 1);
return list;
} static void dfs(List<Integer> list, TreeNode root, int height) {
if (height == list.size())
list.add(root.val);
if (root.right != null)
dfs(list, root.right, height+1);
if (root.left != null)
dfs(list, root.left, height+1); }

Java for LeetCode 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 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

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

  5. (二叉树 bfs) 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 ...

  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 107 Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

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

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

随机推荐

  1. struts2理解

    (1) Struts2(一)---struts2的环境搭建及实例 (2) struts2(二)---ModelDriven模型驱动 (3) Struts2属性驱动与模型驱动 (4)

  2. 【HDU 5399】Too Simple

    题 Description Rhason Cheung had a simple problem, and asked Teacher Mai for help. But Teacher Mai th ...

  3. BZOJ-1206 虚拟内存 Hash+离散化+Priority_Queue

    闻说HNOI每年都有一道Hash. 1206: [HNOI2005]虚拟内存 Time Limit: 50 Sec Memory Limit: 162 MB Submit: 330 Solved: 2 ...

  4. eclipse中使用git

    有的eclipse已经自带了Git了,就不用安装了.如果,想重新安装,可以先卸载GIT,卸载 不同eclipse卸载不一样: 1.在Eclipse中依次点击菜单"Help"-> ...

  5. eclipse下环境变量设置:eclipse导入工程出现 Unbound classpath variable Error

    在导入网友提供的Tomcat源码工程时候出现了 The project cannot be build until build path errors are resolved Unbound cla ...

  6. C/C+小记

    1.struct与typedef struct struct Student{int a;int b}stu1; //定义名为Student的结构体,及一个Student变量stu1 struct { ...

  7. SQL Server中追踪器Trace的介绍和简单使用

    一.What is Trace? 对于SQL Profiler这个工具相信大家都不是很陌生,没用过的朋友可以在SQL Server Management Studio>工具>SQL Ser ...

  8. polling 和 long polling 工作原理

    polling & long polling 参考:http://stackoverflow.com/questions/11077857/what-are-long-polling-webs ...

  9. Linux下/proc目录简介

    文章转载至:http://blog.csdn.net/zdwzzu2006/article/details/7747977 1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在 ...

  10. C++ 中map 中迭代器的简单使用:

    public member function <map> std::map::find iterator find (const key_type& k); const_itera ...