[leetcode]Binary Tree Right Side View
好久不写了,最近忙毕业论文呢。
这个题,就是说一个二叉树,你从右边看,你能看到的数有哪些(会被遮挡)
其实抽象出来就是说。。。二叉树每层最右边的数有哪些。。
那我们按层遍历一次就好了。
/**
* Definition for binary tree
* 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> ans;
if (root == nullptr) return ans;
queue<TreeNode*> que;
que.push(root);
TreeNode* curr;
while(!que.empty()) {
int cnt = que.size();
for (int i = 0; i < cnt; i++) {
curr = que.front(); que.pop();
if (curr->left) {
que.push(curr->left);
}
if (curr->right) {
que.push(curr->right);
}
}
ans.push_back(curr->val);
}
return ans;
}
};
[leetcode]Binary Tree Right Side View的更多相关文章
- [LeetCode] 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 Binary Tree Right Side View (DFS/BFS)
题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...
- leetcode Binary Tree Right Side View python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 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之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】199. Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
随机推荐
- 个性二维码开源专题<替换元素点>
基础方法:ChangeFillShape //修改填充形状 ChangeFillShape(...) // 摘要: // 修改填充形状 // // 参数: // g: // 图形画板 // // Fo ...
- RegexKit: A Regular Expression Toolbox Based On NW.js(Node-Webkit) And Other Greate Open Source Projects.
RegexKit RegexKit: A Regular Expression Toolbox Based On NW.js(Node-Webkit) And Other Greate Open So ...
- Kali Linux Web 渗透测试视频教程— 第四课 google hack 实战
Kali Linux Web 渗透测试— 第四课 google hack 实战 文/玄魂 目录 shellKali Linux Web 渗透测试— 第四课 google hack 实战 课程目录 Go ...
- [游戏模版10] Win32 平面地图贴图 正
>_<:picture resource >_<:If you master the ways of mapping picture,then this problem is ...
- 算法练习26-xx
26.左旋转字符串(字符串) 题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部. 如把字符串abcdef左旋转2位得到字符串cdefab.请实现字符串左旋转的函数.要求时间对长 ...
- C语言实现二叉树-01版
故事是这样开始的,项目经理有一天终于还是拍拍我肩膀说: 无论你的链表写得多么的好,无论是多么的灵活,我也得费老半天才查找到想要的数据: 这让我的工作非常苦恼,听说有一种叫做二叉树的数据结构,你看能不能 ...
- AT&T Assembly for Linux and Mac (sys_write)
Write() in C : (sys_write.c) #include <stdio.h> int main(void) { printf("Hello Landpack\n ...
- Artificial Intelligence Language
Artificial Intelligence Language Objective We know, a true AI program should have ability to underst ...
- paip.抓取网页内容--java php python
paip.抓取网页内容--java php python.txt 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog ...
- [转]Oracle因安装时未设定字符集导致中文乱码的解决方案
在CentOS 6.4上安装Oracle 11g没有设定字符集,采用的是操作系统默认字符集:WE8MSWIN1252,将字符集修改为:AL32UTF8. SQL> select userenv( ...