Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

Input:

    2
/ \
1 3 Output:
1

Example 2:

Input:

        1
/ \
2 3
/ / \
4 5 6
/
7 Output:
7

Note: You may assume the tree (i.e., the given root node) is not NULL.

--------------------------------------------------------------------------------------------------------------------------------

这个题就是求出二叉树的最后一层的最左边的结点的数。

可以用BFS,也可以用DFS,不过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:
int findBottomLeftValue(TreeNode* root) {
if(!root) return ;
queue<TreeNode*> q;
q.push(root);
int res = root->val;
while(!q.empty()){
int len = q.size(); //必须加上这个,如果用i==q.size(),则会出错,因为q.size()会变化。
for(int i = len; i > ; i--){
auto t = q.front();
q.pop();
if(i == len){
res = t->val;
}
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};

(二叉树 BFS) leetcode513. Find Bottom Left Tree Value的更多相关文章

  1. (二叉树 BFS) leetcode993. Cousins in Binary Tree

    In a binary tree, the root node is at depth 0, and children of each depth knode are at depth k+1. Tw ...

  2. Leetcode513. Find Bottom Left Tree Value找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...

  3. LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)

    513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...

  4. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  5. LN : leetcode 513 Find Bottom Left Tree Value

    lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...

  6. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...

  7. 513. Find Bottom Left Tree Value - LeetCode

    Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...

  8. [LeetCode]Find Bottom Left Tree Value

    Find Bottom Left Tree Value: Given a binary tree, find the leftmost value in the last row of the tre ...

  9. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

随机推荐

  1. Vue学习目录

    前面的话 近年来,前端框架发展火热,新的框架和名词不停地出现在开发者眼前,而且开发模式也产生了一定的变化.目前来看,前端MVVM框架的出现给开发者带来了不小的便利,其中的代表就有Angular.js. ...

  2. Express学习(1) ------Express 入门

    Express 是node 第三方框架,大家都知道,框架的意义就在于能大大简化程序地开发.那么我们就看一下Express是怎么简化node程序开发的. 1,用Express写一个hello world ...

  3. poj-1236(强连通分量)

    题意:给你n个点,每个点可能有指向其他点的单向边,代表这个点可以把软件传给他指向的点,然后解决两个问题, 1.问你最少需要给几个点,才能使所有点都能拿到软件: 2.问你还需要增加几条单向边,才能使任意 ...

  4. Civil 3D 二次开发 事务

    事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 对于初学者来说,从字面上难以理解什么是事务.下面我试着通过讲述事务的作用及特性来帮 ...

  5. webpack 打包编译-webkit-box-orient: vertical 后消失

    /* autoprefixer: off */ -webkit-box-orient: vertical; // 参考 https://github.com/postcss/autoprefixer/ ...

  6. Deepfakes:AI换脸技术自制明星XX片

    ps:亮瞎狗眼 去年开始就在国外网站上比较火的项目了,通过Deepfakes技术可以将视频中的人脸换成自己喜欢的明星, 当时就有不少人制作了换脸视频,其中大部分是替换了XX片的女主角. 国外网站Red ...

  7. LOJ2116 [HNOI2015] 开店 【点分治】

    题目分析: 观察题目发现度数不小于三,考虑从边分治入手,用点分治代替.将树划分成重心链接的结构,称为点分树.令当前询问的点为$ u $.那么我们考虑点分树的根到$ u $的一条路径.考虑根结点,排除掉 ...

  8. 【BZOJ1578】【USACO2009Feb】股票市场 背包DP

    题目大意 告诉你\(n\)只股票在这\(m\)天内的价格,给你\(s\)元的初始资金,问你\(m\)天后你最多拥有多少钱. \(n\leq 50,m\leq 10,s\leq 200000,\)答案\ ...

  9. mongoDB 命令整理

    库操作 创建数据库 use [database] 查看数据库 show dbs 删除 db.dropDatabase() 备份 mongodump -h[host] ip -d[databasenam ...

  10. PSR-4 规范实例讲解 -- php 自动加载

    参考 参考文档:https://www.kancloud.cn/thinkphp/php-fig-psr/3144 参考实例:https://github.com/php-fig/fig-standa ...