为什么80%的码农都做不了架构师?>>>   

问题:

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; }
 * }
 */

class Solution{//2ms
    public List<Integer> rightSideView(TreeNode root){
        List<Integer> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null) return res;//必须添加,否则会出现空指针异常
        queue.offer(root);
        while(! queue.isEmpty()){
            int count = queue.size();
            for (int i = 0;i < count ;i ++ ) {
                TreeNode tmp = queue.poll();
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                }
                if (i == count - 1) {
                    res.add(tmp.val);
                }
            }
        }
        return res;
    }   
}

② 用DFS先遍历右子树并记录遍历的深度,如果这个右子节点的深度大于当前所记录的最大深度,说明它是下一层的最右节点(因为我们先遍历右边,所以每一层都是先从最右边进入),将其加入结果中。

class Solution {//1ms
    List<Integer> res = new ArrayList<>();
    int maxDepth = 0;//该参数不能放到方法里面,因为只需要从上到下遍历一次即可,不需要返回。
    public List<Integer> rightSideView(TreeNode root) {
        if (root != null) {
            dfs(root,1);
        }
        return res;
    }
    public void dfs(TreeNode root,int depth){
        if (depth > maxDepth) {
            maxDepth = depth;
            res.add(root.val);
        }
        if(root.right != null) dfs(root.right,depth + 1);
        if(root.left != null) dfs(root.left,depth + 1);
    }
}

③ 也是dfs,不过递归判断条件有一点不同。

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        dfs(root, res, 0);
        return res;
    }
    private void dfs(TreeNode cur, List<Integer> res, int curDepth){
        if(cur == null) return;
        if(curDepth == res.size()) res.add(cur.val);
        dfs(cur.right, res, curDepth + 1);
        dfs(cur.left, res, curDepth + 1);
    }
}

转载于:https://my.oschina.net/liyurong/blog/1572787

记录层序遍历中每层右侧第一个数字 Binary Tree Right Side View相关推荐

  1. PAT甲级——1102 Invert a Binary Tree (层序遍历+中序遍历)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90577042 1102 Invert a Binary Tree ...

  2. leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

    这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...

  3. 二叉树的前中后层遍历

    package com.data.tree;public class BiTree {String data;//数据域BiTree left,right;//下一个结点int leval;//层数 ...

  4. 通过层序遍历和中序遍历构建二叉树

    通过层序遍历和中序遍历构建二叉树的过程与通过中序遍历和先序遍历构建二叉树的过程很相似. 先序遍历和中序遍历是通过先序遍历的首个元素确定根节点,再根据这个元素在中序遍历中出现的位置分为左子树和右子树,然 ...

  5. 力扣刷题之二叉树的层序遍历

                                                      Welcome to you, 每日一刷系列 二叉树的层序遍历 二叉树的层序遍历II 二叉树的右视图 ...

  6. LeetCode 102二叉树的层序遍历103二叉树锯齿形遍历104二叉树的最大深度

    微信搜一搜:bigsai 大家都在关注的刷题.学习数据结构和算法宝藏项目 关注回复进群即可加入力扣打卡群,欢迎划水.近期打卡: LeetCode 97交错字符串(动态规划) LeetCode 98验证 ...

  7. 层序遍历?看这一篇就够了!

    点关注不迷路 1.树的层序遍历 顾名思义,对于树型结构,层序遍历就是按层从上到下,每层按一定顺序对树的节点进行遍历.我们通过如图所示的二叉树进行说明:对于左边的二叉树,按层划分后可得到右边的分层结构. ...

  8. leetcode199. 二叉树的右视图(层序遍历03)

    一:题目 二:上码 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* Tre ...

  9. 由任意二叉树的前序遍历序列和中序遍历序列求二叉树的思想方法_算法与数据结构基础 - 二叉树(Binary Tree)...

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

最新文章

  1. visual Studio 2010 自带报表RDLC动态生成
  2. PN结中存在的Boltzmann常数
  3. java获取接口数据类型_java中调用第三方接口获取数据的方式
  4. 前端,我为什么不要你(转)
  5. python编码程序_python 编码
  6. viewer.js实现预览效果
  7. x86系统MySQL_deepin20.1系统安装MySQL8.0.23(最美国产Liunx系统,最新,最详细的MySQL8安装教程)...
  8. c语言最短延时程序,linux下写个C语言程序,要求有0.5微秒以下的延时,要怎样写...
  9. jQuery Mobile 学习资料
  10. 揭秘一份集团公司的三方数据对接情况
  11. sql server表分区_介绍分区表SQL Server增量统计信息
  12. 作业5.2 5.3
  13. 我开发的kvm虚拟化虚拟机批量生产脚本
  14. Quartus II 13.1入门级使用方法 -仿真篇,适用于小白
  15. MacOS11.6.7上安装Axure9.003720无法预览问题
  16. 自动脚本 android,原神自动脚本全功能版
  17. 推荐系统——Neural Collaborative Filtering(NMF)
  18. Mininet系列实验(七):Mininet脚本实现控制交换机行为
  19. 小米手机刷机为Linux,小米5 刷机LineageOS 14.1的详细教程
  20. Js控制页面刷新(局部刷新全页面刷新)

热门文章

  1. 寻找峰值(局部最大、局部最小)——二分查找
  2. I/O复用函数的使用——poll
  3. vant组件搜索并选择_借助PARTsolutions 选型助手,轻松快速地找到组件。
  4. 构建iOS持续集成平台(三)——CI服务器与自动化部署
  5. windows下远程连接Linux桌面
  6. Index of Java
  7. Set的常用实现类HashSet和TreeSet
  8. OVERLAPPED结构与GetOverlappedResult函数
  9. 【转】Linux中多线程wait使用注意
  10. 1209.1——快速排序算法