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. Struts2拦截器配置

    1. 理解拦截器 1.1. 什么是拦截器: 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AO ...

  2. 初始化一个本地GIT仓储

    简单总结下 // 定位到仓储文件夹目录 $ cd /dir // 初始化本地仓储 $ git init ``` 添加本地GIT忽略清单文件.gitignore```// 添加OS X中系统文件.DS_ ...

  3. UVALive 6911 Double Swords (Set,贪心,求区间交集)

    补:华中VJ这个题目很多标程都不能AC了,包括我下面原本AC了的代码,再交就WA掉了,感觉是样例有问题呢-- 首先左边的是必须要选的,然后右边的需要注意,有些区间是可以舍掉的.1.区间里有两个不同的A ...

  4. push类型消息中间件-消息订阅者(一)

    1.订阅者的声明方式 我们以spring组件化的方式,声明一个消息订阅者,对于消息订阅者关心的主要有: topic: 一级消息类型(又名消息主题).如TRADE 消息类型:二级消息类型,区别同一Top ...

  5. ACdream 1015 Double Kings

    假设第一个人选的点为P,并且当作根,那么第二个人选的最优情况必然是根p连着的那些点中的一个.然后枚举一下P即可. #pragma comment(linker, "/STACK:102400 ...

  6. Failed to read candidate component class包冲突解决方法

    该类问题出现错误有两个可能,第一JAR包错误,第二就是我们的System library版本不正确导致的问题.

  7. 设置radio的选中状态

    $("#s7").click(function () { var a = document.getElementById("s7"); if (a.checke ...

  8. 《JS权威指南学习总结--6.7属性的特性》

    内容要点: 一.ES5中查询和设置属性的API 1.可以通过这些API给原型对象添加方法,并将它们设置成不可枚举的,这让它们看起来更像内置方法. 2.可以通过这些API给对象定义不能修改或删除的属性, ...

  9. ssh: scp命令

    scp 复制命令 Eg. -r /tmp/q1 root@[::]/home 1.命令格式: scp [参数] [原路径] [目标路径] 2.命令功能: scp是 secure copy的缩写, sc ...

  10. Java Object 对象拷贝答疑

    Java Object 对象拷贝答疑 @author ixenos 摘要:在对象的clone过程需要注意的几点.关于关键字this.super 关于clone[对象拷贝] 在实际编程过程,有时候我们会 ...