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:

  1. 2
  2. / \
  3. 1 3

Output:

  1. 1

Example 2:

Input:

  1. 1
  2. / \
  3. 2 3
  4. / / \
  5. 4 5 6
  6. /
  7. 7

Output:

  1. 7

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

BFS Accepted##

这个问题如果按照惯性思维从左到右进行BFS会陷入僵局,但是如果从右往左做BFS就特别简单,最下层的最左值即为最后一次从队列中出来的节点的值。

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int findBottomLeftValue(TreeNode* root) {
  13. queue<TreeNode*> q;
  14. q.push(root);
  15. auto temp = root;
  16. while (!q.empty()) {
  17. temp = q.front();
  18. q.pop();
  19. if (temp->right) q.push(temp->right);
  20. if (temp->left) q.push(temp->left);
  21. }
  22. return temp->val;
  23. }
  24. };

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. Ubuntu 16.04安装TortoiseSVN(基于CrossOver)

    基于CrossOver的TortoiseSVN有如下缺点: 1.不能像Windows下实现右键菜单提交,但是可以通过TortoiseSVN实现迁出代码. 可以解决的问题: 1.可以通过Tortoise ...

  2. 基于图片识别服务的IOS图片识别程序

    由于TensorFlow提供的IOS版Demo相对于Android版识别率不高,所以开发了通过识别服务进行图片识别的IOS版程序. 该程序基于图片识别服务(http://www.cnblogs.com ...

  3. 建立DHCPserver

    一.实验的目的:     实现以下的要求的DHCPserver,了解子网内的IP分配的情况. 二.实验目标 虚拟机 vm1:192.168.6.3/24属于子网VMnet8. 在其上建立DHCPser ...

  4. samba服務器下文件夾chmod權限技巧

    需要的效果: samba下文件夹(abc)不可以被重命名.不可以被刪除,其所有子目录可读可写. 如何做到: chmod 777 -R abc   # -R 使得abc下所有数据继承可读可写权限 chm ...

  5. C++ RTTI介绍

    一.定义:RTTI:Run Time Type Identification ,执行时类型识别:指程序可以使用基类的指针或引用来检索其所指对象的实际派生类型. 二.使用方式:C++中有两个操作符提供R ...

  6. Android 实现形态各异的双向側滑菜单 自己定义控件来袭

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...

  7. HDU 1796 How many integers can you find(容斥原理+二进制/DFS)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  8. GSON学习笔记之初识GSON

    引用"JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,採用全然独立于语言的文本格式.为Web应用开发提供了一种理想的数据交换格式. " ...

  9. cnn,rnn,dnn

    CNN(卷积神经网络).RNN(循环神经网络).DNN(深度神经网络)的内部网络结构有什么区别? https://www.zhihu.com/question/34681168 CNN(卷积神经网络) ...

  10. HDU 5416

    CRB and Tree                                       Time Limit: 8000/4000 MS (Java/Others)    Memory ...