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.

Solution 1:

DFS, preOrder

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int height = 0;
int res = 0;
public int findBottomLeftValue(TreeNode root) {
helper(root, 1);
return res;
} private void helper(TreeNode root, int depth) {
if (root == null) {
return;
}
if (height < depth) {
height = depth;
res = root.val;
}
helper(root.left, depth + 1);
helper(root.right, depth + 1);
}
}

Solution 2:

BFS

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
int res = 0;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
res = cur.val;
if (cur.right != null) {
queue.offer(cur.right);
}
if (cur.left != null) {
queue.offer(cur.left);
}
}
return res;
}
}

[LC] 513. Find Bottom Left Tree Value的更多相关文章

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

  2. 513. Find Bottom Left Tree Value - LeetCode

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

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

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

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

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

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

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

  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. 使用maven打包问题

    项目打包:选择项目 右键->run as-> maven install . 项目中使用的是maven项目,将项目打包成war的时候有时候会出现 出现这种情况的时候解决步骤如下: 选择要打 ...

  2. 简单的Vue计算属性

    倒转字符串 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. Neo4j安装配置(mac)

    Neo4j安装配置(mac) 1.下载APP 注意:无需配置变量 下载地址:https://neo4j.com/download/ 2.安装程序并启动 3.创建数据库(local) 选择版本 4.启动 ...

  4. 为什么声明了int型的变量并且直接初始化后,int型变量的地址一直在变化?

    /************************************************************************* > File Name: ptr_varia ...

  5. 用python批量修改音频ID3等标签

    使用的模块是eyeD3 一.eyeD3的安装 1.安装msgpack,不安装会报错distributed 1.21.8 requires msgpack, which is not installed ...

  6. java链接redis服务器

    1.首先你需要下载驱动包jedis.jar确保下载最新驱动包. 2.public class RedisUtil { //服务器IP地址 private static String ADDR = &q ...

  7. Java学习十七

    学习内容: 1.Java字符串类 1.在utf-8编码下,每个汉字占三个字节 2.字符串和byte数组之间的相互转换 将字符串转换为byte数组 byte[] arrs = str.getBytes( ...

  8. tensorflow笔记(北大网课实战)

    1. tf.multiply(x,y1) # 对应元素相乘 tf.matmul(x,y2) # 矩阵相乘 2.会话:执行计算图中的节点运算的. with tf.Session() as sess: p ...

  9. 解决Android Studio的安装问题

    今天开始了android studio的下载与安装,我再官网上下载了Android studio,下载不难,运行出来可需要一定的时间,在中途中我遇到了一些问题 一:Build错误: 在我最开始下载完A ...

  10. 18 11 05 继续补齐对python中的class不熟悉的地方 和 pygame 精灵

    ---恢复内容开始--- class game : #历史最高分----- 是定义类的属性 top_score =0 def __init__(self, player_name) : #是定义的实例 ...