LeetCode 199. Binary Tree Right Side View(二叉树右侧视图)
2016-05-04 03:32
411 查看
原题网址:https://leetcode.com/problems/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,
You should return
方法:深度优先。
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 { private void rightmost(TreeNode root, int depth, List<Integer> list) { if (depth == list.size()) list.add(root.val); if (root.right != null) rightmost(root.right, depth + 1, list); if (root.left != null) rightmost(root.left, depth + 1, list); } public List<Integer> rightSideView(TreeNode root) { List<Integer> list = new ArrayList<>(); if (root == null) return list; rightmost(root, 0, list); return list; } }
相关文章推荐
- 文章标题
- LeetCode 259. 3Sum Smaller
- 如何使用psql命令去调试含参的sql文件
- LeetCode 198. House Robber(小偷游戏)
- SQLite错误码
- android Camera SurfaceView变形,为什么?
- LeetCode 191. Number of 1 Bits(比特数)
- NYOJ-1070-诡异的电梯[Ⅰ]
- java中的foreach循环
- 门面模式和中介者模式
- LeetCode 190. Reverse Bits(翻转比特)
- NAS
- http笔记
- 关系数据库常用SQL语句语法大全
- linux重定向及nohup不输出的方法
- 冒泡排序的例题讲解
- JDK中的设计模式
- cocos2d-lua+cocos studio之ListView加载Item
- [python]常见数据结构归纳
- 时光隽永,少年何惑