题意:

  给一棵二叉树,要求收集每层的最后一个节点的值。按从顶到底装进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)的更多相关文章

  1. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  2. POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

    POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 ...

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

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

  5. 【LeetCode】563. Binary Tree Tilt 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  6. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  7. 【LeetCode】814. Binary Tree Pruning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 后序遍历 日期 题目地址:https://leetc ...

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

  9. [leetcode]Binary Tree Right Side View

    好久不写了,最近忙毕业论文呢. 这个题,就是说一个二叉树,你从右边看,你能看到的数有哪些(会被遮挡) 其实抽象出来就是说...二叉树每层最右边的数有哪些.. 那我们按层遍历一次就好了. /** * D ...

随机推荐

  1. 【转】数据库范式(1NF 2NF 3NF BCNF)详解二

    以下内容转自:http://jacki6.iteye.com/blog/774889 -------------------------分割线----------------------------- ...

  2. WebAPI用法

    ASP.NET Web API(一):使用初探,GET和POST数据[Parry] HttpClient + ASP.NET Web API, WCF之外的另一个选择[dudu] 通过这两篇文章让我了 ...

  3. 使用strace工具故障排查的5种简单方法

    使用strace工具故障排查的5种简单方法 本文源自5 simple ways to troubleshoot using strace strace 是一个非常简单的工具,用来跟踪可执行程序的系统调 ...

  4. BroadcastReceiver的实例----基于Service的音乐播放器之二

    该程序的后台Service会在播放状态发生改变时对外发送广播(广播将会激发前台Activity的BroadcastReceiver):它也会采用BroadcastReceiver监听来自前台Activ ...

  5. Python中的*args和**kwargs

    原文地址:http://www.linuxidc.com/Linux/2011-10/45083.htm 先来看个例子: ,2,3,4) foo(a=1,b=2,c=3) foo(1,2,3,4, a ...

  6. 大过年的,不下班的,上个Android文件操作类(内部存储和sd卡均可)

    package com.kkdiangame.UI.res; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...

  7. C++封装库

    1.新建项目 -> Win32项目    选择DLL , 勾选 空项目 , 点击完成. 2.本例程,使用一个CPP文件 , 及一个头文件. 其中头文件包含函数声明,CPP文件实现函数声明. 3. ...

  8. Android 系统基础

    当系统启动一个组件,它其实就启动了这个程序的进程(如果这个进程还未被启动的话)并实例化这个组件所需要的类. 例如,如果你的程序启动了相机程序里的activity去拍照,这个activity实际上是运行 ...

  9. touch ImageView

    package com.example.touchdemo; import android.os.Bundle;import android.app.Activity;import android.u ...

  10. 红帽中出现”This system is not registered with RHN”的解决方案

    原因是你的linux没有在红帽网络上注册,所以无法下载上面的软件包,替代方案可以使用centos. 下面介绍下使用centos 的流程 1.卸载rhel的默认安装的yum包查看yum包rpm -qa| ...