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.

Example:

  1. Input: [1,2,3,null,5,null,4]
  2. Output: [1, 3, 4]
  3. Explanation:
  4.  
  5. 1 <---
  6. / \
  7. 2 3 <---
  8. \ \
  9. 5 4 <---

思路

DFS

每当recursion一进入到next level,

就立马加上该level的right side node到result里

对应的,

在recursion的时候,先处理 root.right

代码

  1. class Solution {
  2. public List<Integer> rightSideView(TreeNode root) {
  3. List<Integer> result = new ArrayList<>();
  4. if(root == null) return result;
  5. dfs(root, result, );
  6. return result;
  7. }
  8.  
  9. private void dfs(TreeNode root, List<Integer> result, int level ){
  10. // base case
  11. if(root == null) return;
  12. /*height == result.size() limits the amount of Node add to the result
  13. making sure that once go to the next level, add right side node to result immediately
  14. */
  15. if(level == result.size()){
  16. result.add(root.val);
  17. }
  18. // deal with right side first, making sure right side node to be added first
  19. dfs(root.right, result, level+);
  20. dfs(root.left, result, level+);
  21. }
  22. }

思路

BFS(iteration)

每次先将right side node 加入到queue里去

保证 当i = 0 的时候,poll出来的第一个item是right side node

代码

  1. public List<Integer> rightSideView(TreeNode root) {
  2. // level order traversal
  3. List<Integer> result = new ArrayList();
  4. Queue<TreeNode> queue = new LinkedList();
  5. // corner case
  6. if (root == null) return result;
  7.  
  8. queue.offer(root);
  9. while (!queue.isEmpty()) {
  10. int size = queue.size();
  11. for (int i = 0; i< size; i++) {
  12. TreeNode cur = queue.poll();
  13. // make sure only add right side node
  14. if (i == 0) result.add(cur.val);
  15. // add right side node first, making sure poll out first
  16. if (cur.right != null) queue.offer(cur.right);
  17. if (cur.left != null) queue.offer(cur.left);
  18. }
  19. }
  20. return result;
  21. }

[leetcode]199. Binary Tree Right Side View二叉树右视图的更多相关文章

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

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

  3. leetcode 199 :Binary Tree Right Side View

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

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

  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. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

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

  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 解题报告(Python)

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

随机推荐

  1. [UE4]蓝图函数库小结

    蓝图函数库的功能非常强劲,如果在项目中使用的话有时能达到事半功倍的效果. 蓝图函数库,Blueprint Function Library.可以非常方便的将代码中的函数暴露给所有的蓝图使用,同时也提供 ...

  2. [UE4]C++的const类成员函数

    我们知道,在C++中,若一个变量声明为const类型,则试图修改该变量的值的操作都被视编译错误.例如: const char blank = ‘’; blank = ‘\n’; // 错误 要声明一个 ...

  3. 6.12-PrepareStatement,JdbcUtil 读取数据库配置文件properties,dao模式

    一.PrepareStatement 防止sql注入 PrepareStatement 是预编译sql语句 更加灵活,更有效率 executeUpdate() 做增删改 executeQuery() ...

  4. unity3d动态加载dll的API以及限制

    Unity3D的坑系列:动态加载dll 一.使用限制 现在参与的项目是做MMO手游,目标平台是Android和iOS,iOS平台不能动态加载dll(什么原因找乔布斯去),可以直接忽略,而在Androi ...

  5. python之路之迭代器与生成器

    一  迭代器 那么在研究迭代器之前首先应该要知道什么是迭代. 迭代:是一个重复的过程,并且每次重复都是建立基于上一次的结果而来的,所以在迭代的过程其实是在不断变化的. 迭代器:就是迭代取值的工具. 那 ...

  6. Python - Django - App 的概念

    App 方便我们在一个大的项目中,管理实现不同的业务功能 创建 App: 命令行: python manage.py startapp app名 使用 Pycharm 创建: 文件 -> 新建项 ...

  7. 好用的 FTP 软件之 FileZilla 技巧教程

    FTP 软件之 FileZilla教程 使用教程参考:http://163.26.161.1/~yilinteacher/wwwict/flash/FileZilla.swf (1)如何设置传输完成后 ...

  8. 装机 win7 64 IE11

    英文版win7,更改语言包 英文版 http://windows.microsoft.com/en-us/internet-explorer/download-ie 中文版 http://window ...

  9. 可视化库-seaborn-多变量分析绘图(第五天)

    1. sns.stripplot(x='data', y='total_bill', data=tips, jitter=True), 画出竖形的样子,jitter=True为了使得数据尽量分开 im ...

  10. Spring MVC 异常处理 - ExceptionHandler

    通过HandlerExceptionResolver 处理程序异常,包括Handler映射, 数据绑定, 以及目标方法执行时的发生的异常 实现类如下 /** * 1. 在 @ExceptionHand ...