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 ...
随机推荐
- kindeditor 操作时同步到textarea
KindEditor.ready(function(K) { editor1 = K.create('textarea[name="sponsor"]', { resizeType ...
- 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。
package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...
- VS2010静态编译生成.exe可执行文件
VS2010静态编译生成的.exe可执行文件,可以在其他未安装VS2010的电脑直接运行. 静态编译:就是在编译可执行文件的时候,将可执行文件需要调用的对应动态链接库(.so)中的部分提取出来,链接到 ...
- C++-高效的swap
原始版本: template<typename T> void swap(T& a, T& b) { T tmp(a); a = b; b = tmp; } 此版本不重视效 ...
- 宜家的幸福生活,源于K2 BPM的支撑
很久很久以前,有一篇很火的文章在各大网站被疯狂转载<一个在北欧生活10年的MM,告诉你为什么北欧全球幸福指数第一>,开头第一段就已经让人羡慕嫉妒恨了. "下午的四.五点钟,北欧人 ...
- poj2649 数论
//Accepted 420K 16MS //考虑 0和n! does not divide // 1和0! divides #include <cstdio> #include < ...
- access手工注入
1.判断数据类型 and exists (select * from msysobjects) >0 and exists (select * from sysobjects) >0 一般 ...
- python黑帽子源码
https://www.nostarch.com/download/BHP-Code.zip https://yunpan.cn/cPvLPWMTdWJRu 访问密码 4243
- Python 安全类目推荐 (持续更新)
推荐学习书目 › Learn Python the Hard Way › Python 学习手册 › Python Cookbook › Python 基础教程 Python Sites › PyPI ...
- 10年省赛-Greatest Number (二分+暴力) + 12年省赛-Pick apples(DP) + UVA 12325(暴力-2次枚举)
题意:给你n个数,在里面取4个数,可以重复取数,使和不超过M,求能得到的最大的数是多少: 思路:比赛时,和之前的一个题目很像,一直以为是体积为4(最多选择四次)的完全背包,结果并不是,两两求和,然后二 ...