题目:

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

给出任意一颗二叉树,返回它的每层最右边的结点值集合。

思路:

考虑递归解决该问题。

首先判断根节点是否为空,为空的话直接返回空集合。

递归的获取左右子树的每层的最外侧结点的值集合,如果左边的值集合元素个数多于右边集合的,说明左子树的高度大于右子树的,需要把多的那部分元素加入到右子树的集合中。

代码:

     public static List<Integer> rightSideView(TreeNode root){
List<Integer> arr1 = new ArrayList<Integer>();
List<Integer> arr2 = new ArrayList<Integer>();
//根节点为空返回空集合
if(root == null){
return arr1;
}
arr1.add(root.val);
arr2.add(root.val);
//获取左子树的每层最外侧结点值集合
rightSideView(root.left, arr1);
//获取右子树的每层最外侧结点值集合
rightSideView(root.right, arr2);
//如果左子树集合元素多于右子树,说明左子树高度大于右子树
if(arr1.size() > arr2.size()){
int num = arr2.size();
for(int i = num; i < arr1.size(); i++){
arr2.add(arr1.get(i));
}
}
return arr2;
} //该函数是同样的思想,每次获取左右子树的每层最外侧结点值集合
//如果左子树集合元素多于右子树,说明左子树高度大于右子树
public static void rightSideView(TreeNode root, List<Integer> arr) {
List<Integer> arr1 = new ArrayList<Integer>();
List<Integer> arr2 = new ArrayList<Integer>();
if(root == null){
return;
}
arr1.add(root.val);
arr2.add(root.val);
rightSideView(root.left,arr1);
rightSideView(root.right,arr2);
if(arr1.size() > arr2.size()){
int num = arr2.size();
for(int i = num; i < arr1.size(); i++){
arr2.add(arr1.get(i));
}
}
for(int i :arr2){
arr.add(i);
} }

leetcode_199 Binary Tree Right Side View的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

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

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

  3. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  4. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  5. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  6. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

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

  7. 【刷题-LeetCode】199 Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  8. [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 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. NOIP欢乐模拟赛 T1 解题报告

    小澳的方阵 (matrix.cpp/c/pas) [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑 ...

  2. HDU 3652 B-number(数位DP)

    题目链接 学习大神的数位DP模版. #include <iostream> #include <cstdio> #include <cstring> using n ...

  3. 友盟微博分享Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_CTTelephonyNetworkInfo", referenced from: objc-class-ref in libWeiboSDK.a

    一,分析过程 1.第一次看到这个问题,以为是缺少导入框架或缺少编译文件,导入了微博 sso 框架和编译文件后仍有问题. 2.上网搜了搜也就以上两方面的问题. 3.于是我又仔细看了一遍友盟的分享接口文档 ...

  4. Maven_dependencies 和 dependencyManagement 的区别

    今天我在配置 sellercenter 的接口测试环境的时候,发现一些依赖的写法不太一致: 比如有的依赖的<scope>是写在子项目中的 <dependencies> 下的&l ...

  5. SQL 中怎么查询一个数据库中一共有多少个表

    用户表:select count(*) 总表数 from sysobjects where xtype='u' 总表数:select count(*) 总表数 from sysobject s whe ...

  6. script标签不带属性与带async、defer的区别

    <script> 当页面解析到script标签时,会停止解析并下载对应的脚本,并马上执行,执行完毕后再继续解析页面 <script async> async 在下载脚本的同时不 ...

  7. unity3d插件Daikon Forge GUI 中文教程6-高级控件richtextlabel的使用

    3.5.richtextlabel文本 可以像Word文档一样编辑出多样的内容,图片,字体颜色大小下划线.超链接背景等等. Defaults: 默认字体 默认图集 Blank Texture :空白的 ...

  8. C#面向对象整理

    一.里氏转换 (1)子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替. (2)如果父类装的是子类对象,那么这个父类可以强转为子类对象. 二.值类型跟引用类型区别 1.在内 ...

  9. 关于ASP.NET Web API 客户端的请求报文中添加 Authorization

    当你使用客户端发送请求 Web API 的时候,因为API 有验证,所以你的请求报文中必须有”Authorization“,那么就需要手动添加了! HttpClient client = new Ht ...

  10. 理解group by 语句的扩展使用

    在SQL的开发中我们会经常使用group by语句对数据进行分组统计,然而在一些复杂的BI报表开发中会常遇到更复杂的分组需求,单单使用group by 就不能解决我们的问题了,这时我们就需要学习了解一 ...