LeetCode Binary Tree Right Side View (DFS/BFS)
题意:
给一棵二叉树,要求收集每层的最后一个节点的值。按从顶到底装进vector返回。
思路:
BFS比较简单,先遍历右孩子就行了。
/**
* 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) {
if(root==NULL) return vector<int>();
deque<TreeNode*> que(,root);
vector<int> ans; while(!que.empty())
{
ans.push_back(que.front()->val);
for(int i=que.size(); i>; i--)
{
TreeNode* p=que.front();
que.pop_front();
if(p->right) que.push_back(p->right);
if(p->left) que.push_back(p->left);
}
}
return ans;
}
};
AC代码
DFS比较技巧,需要知道其层次。
/**
* 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> ans;
DFS(ans,root,);
return ans;
}
void DFS(vector<int>& ans,TreeNode* t,int depth)
{
if(t==NULL) return;
if(depth>ans.size()) ans.push_back(t->val);
DFS(ans,t->right,depth+);
DFS(ans,t->left,depth+);
}
};
AC代码
LeetCode Binary Tree Right Side View (DFS/BFS)的更多相关文章
- [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...
- POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...
- 【leetcode】Binary Tree Right Side View(middle)
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- LeetCode OJ: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】563. Binary Tree Tilt 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...
- [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
好久不写了,最近忙毕业论文呢. 这个题,就是说一个二叉树,你从右边看,你能看到的数有哪些(会被遮挡) 其实抽象出来就是说...二叉树每层最右边的数有哪些.. 那我们按层遍历一次就好了. /** * D ...
随机推荐
- java使用dom4j解析xml文件
关于xml的知识,及作用什么的就不说了,直接解释如何使用dom4j解析.假如有如下xml: dom4j解析xml其实很简单,只要你有点java基础,知道xml文件.结合下面的xml文件和java代码, ...
- 重点关注之OData with List
OData是什么 官方解释:The Open Data Protocol (OData) is a data access protocol for the web. OData provides a ...
- 用for循环打印菱形
package nothh; public class mmm { public static void main(String[] args) { //for循环内的 for按顺序运算,先打印1/4 ...
- Phonegap hello world 不容易啊~!
今天一个项目要用phonegap,当初就是觉得phonegap配置太tmd的麻烦了,所以转头appcan,但今天项目必须用-- 先是看到官方说用nodejs装,tmd的,总是重复同一个错误,安装不起, ...
- iOS App创建桌面快捷方式
http://www.cocoachina.com/ios/20150827/13243.html 先mark,暂时用不到
- 数据结构-AVL树
实现: #ifndef AVL_TREE_H #define AVL_TREE_H #include "dsexceptions.h" #include <iostream& ...
- 如何登录Google美国服务器
Google访问须知: ① 先访问一次 https://www.google.com/ncr ,禁止“国家重定向(No country Redirect) ” ② 再点击右上角齿轮图标,选第一项“Se ...
- sql思维
写sql的思路不同于常规编程语言(C.python)等等.前者,考虑如何一步步地得到最终答案:后者,考虑如何一步步地收缩数据范围. 简而言之,前者是面向过程化(for each row do x),后 ...
- UVA 10970-Big Chocolate
题目: 给你一块M*N的巧克力,问把它切成最小单元需要最少切几刀,分开的就不能一起切了. 分析: 每次切割只能多产生一个部分,分成M*N个部分,必然要切M*N-1刀. 一个长为m宽为n的长方形和m*n ...
- Unity截屏
方式一:直接使用unity自带的截图函数 Application.CaptureScreenshot(“imagename”); 保存路径: 在PC上保存路径为Application.dataPath ...