LeetCode OJ: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]
.
其实题目的意思就是相当于二叉树每一行的最右边的一个元素,那么简单,先bfs后取每一个数组的最后一位数就可以了,代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
int dep = -;
bfs(root, dep + );
vector<int> res;
for(int i = ; i < ret.size(); ++i){
res.push_back(ret[i][ret[i].size() - ]);
}
return res;
} void bfs(TreeNode * root, int depth)
{
if(root == NULL) return;
if(depth < ret.size()){
ret[depth].push_back(root->val);
}else{
vector<int>tmp;
ret.push_back(tmp);
ret[depth].push_back(root->val);
}
if(root->left)
bfs(root->left, depth + );
if(root->right)
bfs(root->right, depth + );
}
private:
vector<vector<int>> ret;
};
java版本的代码如下所示,同样是BFS之后再取最后一位组成一个数组:
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
bfs(ret, root, );
for(int i = ; i < ret.size(); ++i){
res.add(ret.get(i).get(ret.get(i).size() - ));
}
return res;
} public void bfs(List<List<Integer>> ret, TreeNode root, int dep){
if(ret.size() <= dep){
ret.add(new ArrayList<Integer>());
}
ret.get(dep).add(root.val);
if(root.left != null)
bfs(ret, root.left, dep + );
if(root.right != null)
bfs(ret, root.right, dep + );
}
}
LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)的更多相关文章
- 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
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- [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 ...
- LeetCode OJ——Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...
- leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...
- 【leetcode】Binary Tree Right Side View(middle)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- Java for 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 ...
- (二叉树 bfs) 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 ...
随机推荐
- Python基础-常用的内置函数
内置函数filter str = ['a', 'b', 'c', 'd'] def fansik(num): if num != "a": return num ret = fil ...
- 系统日志服务rsyslog
一.系统日志服务rsyslog:多线程,可以基于UDP.TCP.TLS协议进行远程通信,还可以将数据存储到MySQL.PGSQL.Oracle,强大的过滤器,可实现过滤日志信息中任何部分,可以自定义输 ...
- python——异常
一.什么是异常 1.错误 从软件方面来说,错误是语法或是逻辑上的.错误是语法或是逻辑上的. 语法错误指示软件的结构上有错误,导致不能被解释器解释或编译器无法编译.这些些错误必须在程序执行前纠正. 当程 ...
- mapInfo文件格式详解
from:http://hotolee.blog.163.com/blog/static/3815229920098434956370 MapInfo以表(Tab)的形式存储信息,每个表是由一组Map ...
- windows下docker toolbox无法下载boot2docker.iso
GitHub连不上导致自动更新失败.(网络形势严峻!) 通过别的途径手动下载了指定的最新的boot2docker.iso文件.(比方说迅雷!比方说迅雷!比方说迅雷!) https://github.c ...
- ThinkPHP框架基础知识一
ThinkPHP是一个快速.兼容而且简单的轻量级国产PHP开发框架,诞生于2006年初,原名FCS,2007年元旦正式更名为ThinkPHP,遵循Apache2开源协议发布,从Struts结构移植过来 ...
- 配置树莓派3和局域网NTP服务器实现内网时间校准
一.配置局域网NTP服务器 1.安装ntp-4.2.8p5-win32-setup.exe 下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm ...
- tcp/ip 中的分组和分片
osi 大家应该都知道osi七层模型吧,物理层 链路层 网络层 传输层 会话层 表示层 应用层ip 属于网络层,tcp 属于传输层,你可以把每一层想像成粽子的粽叶,包裹了七层的粽子最外面的就是物理层, ...
- HNOI2019梦游记
\(Day_0\) 十点半开始睡觉,开始了八个小时的不眠之夜,整晚都没睡着,这状态明天肯定挂了 \(Day_1\) 开局一条鱼,计算几何只会\(20\) 还是\(T2\)的\(20\)纯暴力好打,\( ...
- hadoop实战项目:查找相同字母组成的字谜
前面我们学习了MapReduce编程思想和编程示例,那么本节课程同学们一起操练操练,动手完成下面的项目. 项目需求 一本英文书籍包含成千上万个单词或者短语,现在我们需要在大量的单词中,找出相同字母组成 ...