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

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

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

Java代码:

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public List
rightSideView(TreeNode root) { List result = new ArrayList(); if(null == root) return result; List result_left = rightSideView(root.left); List result_right = rightSideView(root.right); result.add(root.val); result.addAll(result_right); if(result_left.size() > result_right.size()){ result.addAll(result_left.subList(result_right.size()-1,result_left.size-1)); } return result; }}
 

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

你可能感兴趣的文章
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
ZUUL2 使用场景
查看>>
Spring AOP + Redis + 注解实现redis 分布式锁
查看>>
支付宝生活号服务号 用户信息获取 oauth2 登录对接 springboot java
查看>>
CodeForces #196(Div. 2) 337D Book of Evil (树形dp)
查看>>
uva 12260 - Free Goodies (dp,贪心 | 好题)
查看>>
uva-1427 Parade (单调队列优化dp)
查看>>
【设计模式】学习笔记14:状态模式(State)
查看>>
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
斯坦福大学机器学习——因子分析(Factor analysis)
查看>>
项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved
查看>>
linux对于没有写权限的文件如何保存退出vim
查看>>
Windows下安装ElasticSearch6.3.1以及ElasticSearch6.3.1的Head插件
查看>>
IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
查看>>