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 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的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- 【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, ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【刷题-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, ...
- [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 ...
- [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 ...
随机推荐
- UVA 10892 - LCM Cardinality(数学题)
题目链接 写写,就ok了. #include <cstdio> #include <cstring> #include <string> #include < ...
- InterBase数据库迁移到MySQL(恢复备份)
我拿到的是InterBase导出的“.gbk”后缀的数据库备份文件,目标是可以通过命令行的方式导入到指定的数据库中,在这个脚本中我使用了InterBase数据库中自带的“gbak”命令行来进行操作. ...
- Install the Maven in your computer
While, this blog will talk about installing the Maven in your computer. There are three steps as fol ...
- BFS 巡逻机器人
巡逻机器人 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/F 题目大意: 机器人在一个矩形区域巡逻, ...
- 如何打印出lua里table的内容
不像开发as3时用fb有强大的断点调试功能,一般lua开发不用什么高级的ide,貌似也没有适合的,就直接用sublime.exvim等文本编辑器,直接编译运行看结果.所以不能很方便的知道变量值,特别是 ...
- 移位运算符(JAVA)
java中有三种移位运算符 << : 左移运算符,num << 1,相当于num乘以2 >> : 右移运算符,num >&g ...
- OpenCV 3.1 StereoBM 获取正确视差Dispariy
OpenCV更新到3.0版本后,Stereo模块变化的挺多的,首先去掉了StereoBMState和StereoSGBMState这两个专门控制BM和SGBM算法参数的类,而且StereoBM不能直接 ...
- Daily Scrum 10.29
今天大家的工作做的还算不错,但是晚些时候遗憾的得知我们的吴文会同学生病住院了,所以她明天的任务暂时保留,再做调整.希望大家在努力学习工作的同时一定要注意身体啊! 下面是今天的Task统计:
- VPN fq工具的选择
豆荚VPN还是不错的.有时候百度会打不开,重新连接一下就可以了 http://wandou.shouyo99.com/ 如果高速模式不可以,请记得选择PPTP模式!!!但有个副作用就是百度打不开了--
- html - 自动播放音乐
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...