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 ...
随机推荐
- 0401-服务注册与发现、Eureka简介
一.硬编码问题 解决方案:nginx.或.服务注册与发现 二.服务发现 注册.心跳机制 三.服务发现组件的功能 1.服务注册表:是一个记录当前可用服务实例的网络信息的数据库,是服务发现机制的核心.服务 ...
- 002-unity3d插件使用
一.导入第三方的工具包(.unitypackage文件) 1.NGUI项 NGUI是一款收费的插件,在Asset Store中大家可以看到价格.在未购买正版的前提下我们可以通过两种方法来使用NGUI, ...
- 判断IP地址是否合法
/* return 1 if string contain only digits, else return 0 */ int valid_digit(char *ip_str) { while (* ...
- C# 函数4
//数据库 public class GF_DA { /// <summary> /// 执行SQL语句 sConnStr 连接字符串,sq ...
- python之路 正则表达式,模块导入的方法,hashlib加密
一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...
- UI控件之UICollectionView
UICollectionView:集合视图,是iOS6.0后出现的,与UITableView类似,优势在于可以灵活的布局cell UICollectionViewLayout:布局类,抽象类,一般定义 ...
- Spring_HelloWord
环境:IntelliJ 14 : jdk1.8 Spring操作步骤 1.新建项目---Spring Batch 2.IntelliJ会自动加载jar包 3.现在就可以在src目录下写Java类文 ...
- $Android AlarmManager的用法详解
在Android的Alarm机制中,使用AlarmManager可以实现类似闹钟这样的定时任务.在毕业设计项目中要实现定时任务的功能,所以在这里先进行一下梳理. (一)AlarmManager与Bro ...
- GZDBHelper
NuGet:GZDBHelper 初始化: public class APIBase : ApiController { protected GZDBHelper.IDatabase db; publ ...
- Centos系统 上下文切换的检查思路
1.什么是上下文切换(Context Switch)? 上下文切换,有时也称做进程切换或任务切换,是指CPU从一个进程或线程切换到另一个进程或线程. 操作系统可以同时运行多个进程, 然而一颗CPU同时 ...