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)的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. (二叉树 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 ...

  8. 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, ...

  9. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

随机推荐

  1. -webkit-text-size-adjust: none;该如何处理

    -webkit-text-size-adjust: none; 在中文版Chrome里面,网页CSS里所有小于12px的字体设置都无效,最终将显示12px.这样弄的本意可能 是好的,因为中文一旦小于1 ...

  2. 如何保护 .NET 应用的安全?

    自从 Web 应用能给访问者提供丰富的内容之后,黑客们就把目光转向任何他们能够破坏,损毁,欺骗的漏洞.通过网络浏览器提供的应用越来越多,网络罪犯们可以利用的漏洞数量也呈指数增长起来. 大多数企业都依赖 ...

  3. 程序自动生成Dump文件

    前言:通过drwtsn32.NTSD.CDB等调试工具生成Dump文件, drwtsn32存在的缺点虽然NTSD.CDB可以完全解决,但并不是所有的操作系统中都安装了NTSD.CDB等调试工具.了解了 ...

  4. 【流媒體】live555—VS2008 下live555编译、使用及测试

    [流媒體]live555—VS22008 下live555编译.使用及测试 Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如R ...

  5. Qt的版本历史

    发展史 Qt的第一个商业版本于1995年推出,随后Qt的发展就很快了,下面是Qt发展史上的一 些里程碑,从之前的Qt1.x开始到现在的Qt5.x. Qt1-3 版本 发布日期 1.40 10 July ...

  6. Linux 下常用命令

    linux 下常用命令: 1.删除文件命令为 rm 2.创建目录的命令是:mkdir 3.删除目录的命令是rmdir(空目录) 4.切换到root帐号:su 5.查看所有进程:ps -aux 6.杀死 ...

  7. 如何提高Web服务端并发效率的异步编程技术

    作为一名web工程师都希望自己做的web应用能被越来越多的人使用,如果我们所做的web应用随着用户的增多而宕机了,那么越来越多的人就会变得越来越少了,为了让我们的web应用能有更多人使用,我们就得提升 ...

  8. linux中应用程序main函数中没有开辟进程的,它应该在那个进程中运行呢?

    1.main函数是一个进程还是一个线程? 不知道你是用c创建的,还是用java创建的. 因为它们都是以main()做为入口开始运行的. 是一个线程,同时还是一个进程. 在现在的操作系统中,都是多线程的 ...

  9. mac terminal终端ls命令参数详解

    原文:https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/ls.1.html -a ...

  10. HHVM 是如何提升 PHP 性能的?

    背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...