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的作用域
Python的作用域 转自:http://www.cnblogs.com/frydsh/archive/2012/08/12/2602100.html Python是静态作用域语言,尽管它自身是一个动 ...
- Kerbose
http://blog.csdn.net/wulantian/article/details/42418231
- 将ANGULAR与后端请求结合
简单的结合,却是很多应用的基础.RESTFUL就此而生.瘦服务,富客户. <!DOCTYPE html> <html lang="en" ng-app=" ...
- hdu 1907 John
很简单的博弈论问题!!(注意全是1时是特殊情况) 代码如下: #include<stdio.h> #include<iostream> using namespace std; ...
- Java在Windows的环境配置
JDK环境变量配置的步骤如下: 1.我的电脑-->属性-->高级-->环境变量. 2.配置用户变量: 系统变量 a.新建 JAVA_HOME C:\Program Files\Jav ...
- jmeter 响应结果分析一
转自:http://www.cnblogs.com/Carrie_Liang/archive/2008/11/05/1327604.html Jmeter测试结果分析这一篇,我打算分成上下两部分.上篇 ...
- EntityFreamwork 读写分离
http://www.cnblogs.com/cjw0511/p/4391092.html
- ZOJ1204——Additive equations(DFS)
Additive equations Description We all understand that an integer set is a collection of distinct int ...
- imports,using,和include之间的区别
Imports, Using基本一样,有两个作用 1.将后面命名空间中所有的名字导入到当前命名空间 2.为后面的名字取一个当前命名空间可以访问的别名. 比如StreamWriter这个类在System ...
- 为Windows 7添加“Internet打印”功能
http://wangchunhai.blog.51cto.com/225186/1156589/