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) {
List<Integer> result = new ArrayList<Integer>();
if(root == null){
return result;
}
helper(root, result, 1); return result;
}
private void helper(TreeNode root, List<Integer> list, int lvl){
if(root == null){
return;
}
if(list.size() < lvl){
list.add(root.val);
}
helper(root.right, list, lvl + 1);
helper(root.left, list, lvl + 1);
}
}

这个方法采用了递归实现,helper有三个参数传入,一个是当前visit的节点,一个是存储结果的list,还有就是代表当前遍历到哪一层的lvl。这个题目实际上就是找出每一层最靠右边的节点,因此每一层只取一个数。当访问一个节点时,如果发现当前层数比list.size()大,那个这个节点就是最右边的节点,并把该节点加入到list中,然后从该节点的右子树向下递归,再从该节点的左子树向下递归。

LeetCode OJ 199. Binary Tree Right Side View的更多相关文章

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

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

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

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

  4. LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

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

  5. leetcode 199 :Binary Tree Right Side View

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

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

  7. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  8. [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. 常用到的简单命令 Sublime Git NPM WindowsCMD MacTerminal(Unix命令)

    sublime 选择标签及其内容 ctrl+shift+a连续按两次 Git 撤销 add 重置暂存区的指定文件,与上一次commit保持一致,但工作区不变 git reset [file] 撤销 c ...

  2. MapReduce库类

    Hadoop除了可以让开发人员自行编写map函数和reduce函数,还提供一些常用函数(mapper.reducer和partitioner)的类库,这些类位于 org.apache.hadoop.m ...

  3. 使用JS通过正则限制input的输入

    第一: 限制只能是整数 type = "text" name= "number" id = 'number' onkeyup= "if(! /^d+$ ...

  4. SpringMVC之ModelAndView的简单使用

    可以使用ModelAndView来跳转页面和传值,具体用法如下: 构造方法的参数是要跳转的页面! 通过 ModelAndView 的对象的 AddObject(K,V)方法,可以传入数据! 获得mod ...

  5. final使用

    final修饰  基本数据类型时候   对应的 数据不能改变:::final修饰 对象类型 ,那么对应的引用地址不能改变(对象中的值可以改变): 如果final修改方法,那么该方法不能被子类重写 :: ...

  6. Maze

    Maze 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5094 BFS+状态压缩 把身上携带钥匙的状态压缩成一个2^10的整数.这道题难在 ...

  7. 2、FileOutputStream--->文件输出流(向文件写入数据)

    Api介绍 定义 FileOutputStream 用于写入诸如图像数据之类的原始字节的流.要写入字符流,请考虑使用 FileWriter. 构造方法 FileOutputStream(File fi ...

  8. background-clip与background-origin两者的区别

    第一篇随笔有提到 background-clip与background-origin两者的区别,但是太字面化了,对于小白而言甚是难以理解,包括我自己,在第二次去理解的时候,反而蒙圈了.所以,查阅了一些 ...

  9. Java JVM 内存泄漏--全解析和处理办法 [ 转载 ]

    Java JVM 内存泄露——全解析和处理办法 [转载]   @author 小筐子 @address http://www.jianshu.com/p/bf159a9c391a         JA ...

  10. 官方解答:Vultr VPS常见问题

    VULTR VPS配置高,价格低廉,是非常优秀的vps品牌.今天我来翻译vultr官方FAQ,相信你能找到具体答案. Q 请介绍VULTR VPS机器硬件配置 Intel CPU 3+ GHz Cor ...