当前位置: 首页 > news >正文

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

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

 

You should return [1, 3, 4].

链接: http://leetcode.com/problems/binary-tree-right-side-view/

题解:

求二叉树right side view。 我们可以使用层序遍历,每次当前level == 0时,这个节点为此层最右边的节点,这时候我们把这个节点的value加入到结果集去。应该还有很多更好的方法,二刷要再细致一些。

Time Complexity - O(n), Space Compleixty - O(n)。

/**
 * 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> res = new ArrayList<>();
        if(root == null)
            return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int curLevel = 1, nextLevel = 0;
        
        while(!queue.isEmpty()) {
            TreeNode node = queue.poll();
            curLevel--;
            if(node.left != null) {
                queue.offer(node.left);
                nextLevel++;
            }
            if(node.right != null) {
                queue.offer(node.right);
                nextLevel++;
            }
            if(curLevel == 0) {
                curLevel = nextLevel;
                nextLevel = 0;
                res.add(node.val);
            }
        }
        
        return res;
    }
}

 

二刷:

依然是使用level order traversal,每次当curLevel = 0的时候,我们把当前node.val加入到结果集中。

也可以使用其他方法。下一次要补上recursive的。

Java:

/**
 * 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> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int curLevel = 1, nextLevel = 0;
        while (!q.isEmpty()) {
            TreeNode node = q.poll();
            curLevel--;
            if (node.left != null) {
                q.offer(node.left);
                nextLevel++;
            }
            if (node.right != null) {
                q.offer(node.right);
                nextLevel++;
            }
            if (curLevel == 0) {
                curLevel = nextLevel;
                nextLevel = 0;
                res.add(node.val);
            }
        }
        return res;
    }
}

 

三刷:

Java: 

Recursive:

主要是使用一个int变量depth来控制递归。在当前深度等于list.size()时,我们加入root节点的值。因为每个level只取一个最右节点,所以我们先递归求解右子树,再求解左子树。

Time Complexity - O(n), Space Compleixty - O(n)。

/**
 * 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> res = new ArrayList<>();
        rightSideView(res, root, 0);
        return res;
    }
    
    private void rightSideView(List<Integer> res, TreeNode root, int depth) {
        if (root == null) return;
        if (depth == res.size()) res.add(root.val);
        rightSideView(res, root.right, depth + 1);
        rightSideView(res, root.left, depth + 1);
    }
}

 

 

Reference:

https://leetcode.com/discuss/31348/my-simple-accepted-solution-java

https://leetcode.com/discuss/40084/java-solution-using-recursion 

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Ubuntu系统SSH远程服务器查看日志显示乱码问题解决
  • 光线凭借《左耳》胜出五一档后,又要拉上奇虎360整大事?
  • 冲刺阶段站立会议每天任务7
  • 常用软件安装及VS插件工具
  • 【OpenWRT之旅】LuCI探究
  • 幂集问题
  • 分析nginx ip地址来源
  • [转] 常用的正则表达式全面总结
  • GTK+(2)--窗口中添加五脏六腑
  • 【Android】Android自定义View和组合控件
  • sqlServer将多字段设为主键方法
  • UITableView多选全选
  • 简述WebService与.NET Remoting的区别及适应场合 WCF
  • C#为工作Sql而产生的字符串分割小工具(很实用,你值得拥有)
  • mongodb安装-配置文件
  • [译]如何构建服务器端web组件,为何要构建?
  • 4. 路由到控制器 - Laravel从零开始教程
  • C++类的相互关联
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • MySQL的数据类型
  • oldjun 检测网站的经验
  • PermissionScope Swift4 兼容问题
  • python学习笔记 - ThreadLocal
  • Web Storage相关
  • 产品三维模型在线预览
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 力扣(LeetCode)22
  • 详解移动APP与web APP的区别
  • 《天龙八部3D》Unity技术方案揭秘
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​虚拟化系列介绍(十)
  • $(selector).each()和$.each()的区别
  • (10)ATF MMU转换表
  • (20050108)又读《平凡的世界》
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (二)换源+apt-get基础配置+搜狗拼音
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (过滤器)Filter和(监听器)listener
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • **《Linux/Unix系统编程手册》读书笔记24章**
  • **CI中自动类加载的用法总结
  • ... 是什么 ?... 有什么用处?
  • .NET NPOI导出Excel详解
  • .net 托管代码与非托管代码
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑
  • /proc/stat文件详解(翻译)
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决
  • [2669]2-2 Time类的定义
  • [7] CUDA之常量内存与纹理内存
  • [BUUCTF]-PWN:[极客大挑战 2019]Not Bad解析
  • [C/C++] -- 二叉树
  • [C++][基础]1_变量、常量和基本类型
  • [CC2642r1] ble5 stacks 蓝牙协议栈 介绍和理解
  • [CSS] 点击事件触发的动画