leetcode@ [199] Binary Tree Right Side View (DFS/BFS)
https://leetcode.com/problems/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]
.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class node {
public:
TreeNode *nd;
int lv;
node(TreeNode *rhs, int l): nd(rhs), lv(l) {}
}; class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<pair<int, int> > load;
vector<int> res;
if(root == NULL) return res; stack<node> q;
q.push(node(root, ));
int lv = ; while(!q.empty()) {
node top = q.top();
int cur_lv = top.lv;
q.pop();
load.push_back(make_pair(top.nd->val, cur_lv)); if(top.nd->left) q.push(node(top.nd->left, cur_lv+));
if(top.nd->right) q.push(node(top.nd->right, cur_lv+));
} int elv = ;
for(int i=; i<load.size(); ++i) {
if(elv == load[i].second) {
res.push_back(load[i].first);
++elv;
}
} return res;
}
};
leetcode@ [199] Binary Tree Right Side View (DFS/BFS)的更多相关文章
- 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 二叉树的右侧视图
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 ...
- 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 ...
- [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 ...
- leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
随机推荐
- Python 异常结构
http://flyheaven.blog.163.com/blog/static/7401172201193085243920/ 1.Python内建异常体系结构 The class hierarc ...
- Data transfer object
Data transfer object (DTO) is a design pattern used to transfer data between software application su ...
- iphone绘图的几个基本概念CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
我一般情况下不会使用interface builder去画界面,而是用纯代码去创建界面,不是装B,而是刚从vi转到xcode不久,不太习惯interface builder而已.当然如果需要我也会使用 ...
- linux Ubuntu12 设置root用户登录图形界面
Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...
- http://doc.okbase.net/congcong68/archive/112508.html
http://doc.okbase.net/congcong68/archive/112508.html
- [java]2015上海邀请赛 B Base64
题意: 给n和一个字符串(可以有空格) 求字符串编码n次后的字符串 编码方式:字符串的每个字符转化成ASCII码, ASCII码转化成8位2进制, 将二进制数分割成6位为一组的(不够的补0), ...
- 使用typeid(变量或类型).name()来获取常量或变量的类型---gyy整理
使用typeid(变量或类型).name()来获取常量或变量的类型 <typeinfo> 该头文件包含运行时类型识别(在执行时确定数据类型)的类 typeid的使用 typeid操作 ...
- MYSQL 优化建议
转自 http://coolshell.cn/articles/1846.html MYSQL 优化建议20条 1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效 ...
- PLS-00103: 出现符号 ...
Oracle存储过程: create or replace procedure update_people(in_name ), in_status in nvarchar2) as begin up ...
- Razor视图引擎的基本概念与法语
Razor 视图引擎的特点: 简洁.富于表现.流畅 尽量减少页面代码的输入,实现快速流畅的编程工作 不必明确为服务器代码标记起始与结束符,Razor 能智能判断,这样让页面看清洁,代码方便阅读 asp ...