博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
199. Binary Tree Right Side View
阅读量:7055 次
发布时间:2019-06-28

本文共 859 字,大约阅读时间需要 2 分钟。

问题:

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

解答:

核心思想是每一层只取一个结点,所以result的大小与高度是一样的。

public class Solution {    public void Helper(TreeNode root, List
result, int curLength) { if (root == null) return; if (curLength == result.size()) { result.add(root.val); } Helper(root.right, result, curLength + 1); Helper(root.left, result, curLength + 1); } public List
rightSideView(TreeNode root) { List
result = new ArrayList
(); Helper(root, result, 0); return result; }}

转载地址:http://vglol.baihongyu.com/

你可能感兴趣的文章
高效能人士的7个习惯
查看>>
mysql不常用但很有用的语句整理
查看>>
MySQL存储过程示例
查看>>
格式化输出%s和%S的区别
查看>>
微信小程序即将上线,创业者机会在哪里?
查看>>
_mysql_exceptions.IntegrityError: (1062, "Duplicate entry, Python操作MySQL数据库,插入重复数据...
查看>>
spring boot和maven的约定大于配置体现在哪些方面
查看>>
蒙提霍尔游戏 python 模拟
查看>>
cpython和lua源码阅读
查看>>
JavaEE开发之SpringMVC中的路由配置及参数传递详解
查看>>
抽屉式导航可能减少产品一半的用户參与度
查看>>
002_docker构建zookeeper环境
查看>>
要点Java20 java.util.Collections
查看>>
KETTLE集群搭建
查看>>
Linux实战案例(6)yum查找、卸载、和安装软件
查看>>
3-06. 表达式转换(25)(中缀表达式转后缀表达式ZJU_PAT)
查看>>
Oracle 12c 新特性之 temp undo
查看>>
css中,如何设置前景色的透明度?
查看>>
SQL Server2008附加数据库之后显示为只读时解决方法
查看>>
USACO Section 2.1 Healthy Holsteins
查看>>