lc 513 Find Bottom Left Tree Value


513 Find Bottom Left Tree Value

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 Accepted##

这个问题如果按照惯性思维从左到右进行BFS会陷入僵局,但是如果从右往左做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:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
auto temp = root;
while (!q.empty()) {
temp = q.front();
q.pop();
if (temp->right) q.push(temp->right);
if (temp->left) q.push(temp->left);
}
return temp->val;
}
};

LN : leetcode 513 Find Bottom Left Tree Value的更多相关文章

  1. [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  2. 513. Find Bottom Left Tree Value - LeetCode

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

  3. 【LeetCode】513. Find Bottom Left Tree Value 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS Date 题目地址:https:// ...

  4. 【leetcode】513.Find Bottom Left Tree Value

    原题 Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / 1 ...

  5. 513 Find Bottom Left Tree Value 找树左下角的值

    给定一个二叉树,在树的最后一行找到最左边的值. 详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/ C+ ...

  6. [LC] 513. Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

  7. 513. Find Bottom Left Tree Value

    Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...

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

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

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

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

随机推荐

  1. HDU——1285 确定比赛名次

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. Ubuntu 16.04安装TeamViewer

    安装i386库: sudo apt-get install libc6:i386 libgcc1:i386 libasound2:i386 libexpat1:i386 libfontconfig1: ...

  3. JSP的Cookie处理

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/Cookies-handling.html: Cookies是存储在客户端计算机的文本文件,保存各种跟踪目 ...

  4. php-fpm配置项

    php版本:php-7.3 全局配置 ;;;;;;;;;;;;;;;;;;;;; ; FPM Configuration ; ;;;;;;;;;;;;;;;;;;;;; ; All relative ...

  5. JAVA调用动态链接库(dll)

        菜鸡爬坑 基础知识  因为某个东西的keygen我只会在win下生成!! 所以只能出此下策!!之前一直是android下用jni调用so文件,现在试下java在win平台下调用dll 首先还是 ...

  6. FDMemTable内存表操作

    unit Umemtable; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System ...

  7. 第一个GraphX程序

    程序功能:收集顶点指向的邻居中所在地 /* * 找出每一个顶点所指向的邻居中所在的地区 */ import org.apache.spark.SparkContext import org.apach ...

  8. ios31--NSThread

    // // ViewController.m // 03-掌握-NSThread基本使用 #import "ViewController.h" #import "XMGT ...

  9. Flask的多app应用,多线程如何体现

    一.多app应用 在一个py文件中创建多个Flask的app对象 from werkzeug.wsgi import DispatcherMiddleware from werkzeug.servin ...

  10. python-----自动解压并删除zip文件

    如何自动解压并删除zip? 如何解压  →  使用内置模块来实现(shutil.unpack_archive) 如何删除zip  →  使用内置模块os来实现(os.remove) 如何监测zip的出 ...