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.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation: 1 <---
/ \
2 3 <---
\ \
5 4 <---
--------------------------------------------------------------------------------------------------
水。。。。。。用bfs就可以快速先解决掉了 C++代码:
/**
* 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) {
vector<int> vec;
if(!root) return vec;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
for(int i = q.size(); i > ; i--){
auto t = q.front();
q.pop();
if(i == ){
vec.push_back(t->val);
}
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return vec;
}
};

(二叉树 bfs) leetcode 199. Binary Tree Right Side View的更多相关文章

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

  6. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

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

  9. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

随机推荐

  1. Apache ab 单测 分布式

    使用synchronized 处理并发 缺点:无法做到细粒度控制 只适合单点的情况 使用Redis作为分布式锁 setnx命令 设计模式 :使用 !setnx 加锁 getset命令

  2. css 媒体查询 注意点

    1, 媒体查询表达式之间还可以用逗号,@media (max-width:800px), print  它表示或的意思 @media (max-width: 800px) OR print; 2, n ...

  3. Nginx 当上游服务器返回失败时的处理办法

    陶辉95课 Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503  ...

  4. 高度可配置的 Linux 内存守护程序 Nohang!

    导读 Nohang 是一个 Linux 守护程序,也是一个高度可配置的 OOM(内存溢出)阻止工具,适用于 Linux 系统,能够有效地防止内存不足的情况. 部分功能特性 具有良好注释的配置文件,配置 ...

  5. IntelliJ IDEA 取消【import .;】星号导包

    Setting -- Editor -- Code Style -- Java -- Imports 在 在 具体数值自行填写,够大即可,截图如下:

  6. crawlspider_房多多

    框架写起来代码是真的简洁多了,还有就是在requests爬取房多多的时候,无法爬取所有地区,而这个就不受影响 代码请查看码云 运行结果:

  7. VueCLI3如何更改安装时的包管理器为yarn或npm

    在执行 vue create project 后如果显示如下 npm run serve 则表示你使用的是npm创建的项目. 如果显示如下 yarn serve 则表示此项目为yarn创建. 那如何切 ...

  8. 各种反演细节梳理&模板

    炫酷反演魔术课件byVFK stO FDF Orz(证明全有%%%) 莫比乌斯反演 \(F(n)=\sum\limits_{d|n}f(d)\Rightarrow f(n)=\sum\limits_{ ...

  9. 【BZOJ5292】[BJOI2018]治疗之雨(高斯消元)

    [BZOJ5292][BJOI2018]治疗之雨(高斯消元) 题面 BZOJ 洛谷 题解 设\(f[i]\)表示剩余\(i\)点生命时的期望死亡的次数. 考虑打\(k\)次下来脸上被打了\(i\)下的 ...

  10. poj1845 sumdiv (因数的和)

    首先分解质因数,$A^B=p_1^{m_1B}p_2^{m_2B}...p_n^{m_nB}$ 然后的话,它的所有因数的和就是$\prod{(1+p_i^1+p_i^2+...+p_i^n)}$ 用一 ...