Question

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

先求出树的高度,然后层次遍历,遍历到最后一层,输出最左边的节点(也就是最后一层第一个节点,因为层次遍历采用从左到右的方式)。

Code

/**
* 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) {
int height = calcTreeHeight(root);
int count = 1;
queue<TreeNode*> childs, new_childs;
childs.push(root);
while (1) {
while(!childs.empty()) {
TreeNode* tmp = childs.front();
if (count == height)
return tmp->val;
childs.pop();
if (tmp->left != NULL)
new_childs.push(tmp->left);
if (tmp->right != NULL)
new_childs.push(tmp->right);
}
count++;
childs = new_childs;
clear(new_childs);
} }
// 清空队列的方法
void clear( std::queue<TreeNode*> &q )
{
std::queue<TreeNode*> empty;
std::swap(q, empty );
}
int calcTreeHeight(TreeNode* root) {
if (root == NULL)
return 0;
int left = calcTreeHeight(root->left);
int right = calcTreeHeight(root->right);
return max(left, right) + 1;
}
};

Solution 2

直接层次遍历也可以,最后一层遍历结束后,直接输出最后一层的第一个节点即可。

Code 2

/**
* 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*> childs, new_childs;
childs.push(root);
while (1) {
int flag = 1;
TreeNode* first;
while(!childs.empty()) {
TreeNode* tmp = childs.front();
if (flag) {
first = tmp;
flag = 0;
}
childs.pop();
if (tmp->left != NULL)
new_childs.push(tmp->left);
if (tmp->right != NULL)
new_childs.push(tmp->right);
}
if (new_childs.empty()) {
return first->val;
}
childs = new_childs;
clear(new_childs);
} }
void clear( std::queue<TreeNode*> &q )
{
std::queue<TreeNode*> empty;
std::swap( q, empty );
}
};

LeetCode——Find Bottom Left Tree Value的更多相关文章

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

  2. [LeetCode] 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. leetcode算法: Find Bottom Left Tree Value

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

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

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

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

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

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

  7. 513. Find Bottom Left Tree Value - LeetCode

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

  8. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  9. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

随机推荐

  1. java 泛型没有协变类型, 所以要重用extends, 但使用List<? extends Fruit> 可以是ArrayList<Fruit>()、ArrayList<Apple>()、ArrayList<Orange>(), 因此不能add元素进去

    class Fruit{} class Apple extends Fruit{} class SubApple extends Apple{} class Orange extends Fruit{ ...

  2. getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...

  3. python就业班-淘宝-目录.txt

    卷 TOSHIBA EXT 的文件夹 PATH 列表卷序列号为 AE86-8E8DF:.│ python就业班-淘宝-目录.txt│ ├─01 网络编程│ ├─01-基本概念│ │ 01-网络通信概述 ...

  4. python len() 函数

    Python len() Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. len(obj) 方法语法 obj -- 对象(字符串.列表.元组.字典等) 字符串长度 > ...

  5. 005-redis-命令-无序集合,有序集合

    Redis 无序集合命令 下表列出了 Redis 集合基本命令: 序号 命令及描述 1 SADD key member1 [member2] 向集合添加一个或多个成员 2 SCARD key 获取集合 ...

  6. github多人协同使用。

    点击 一:自己跟随别人的项目进行开发 1:首先登陆github,找到自己协同开发的项目. 例如:CrossMountain 的we-pay项目 ,点击  fork,该项目就在自己的账号下面了. 2:在 ...

  7. NGINX负载均衡缓存配置

    环境:VMware-Workstation-12-Pro,Windows-10,CentOS-7.5,Xshell5 1 概述 如果我们的架构是前端负载均衡后端WEB集群时,可以开启nginx的缓存功 ...

  8. selenium webdriver 截屏操作

    有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait ...

  9. 用户用户组管理:用户管理命令-passwd

    passwd直接回车就是给root设密码.或加root. 普通用户只能改自己的密码.改时直接敲passwd,回车.否则报错. 因为只有root可以在passwd后加用户名.其实最常见的就是不加选项. ...

  10. 面经:Bloomberg Internship第一轮

    上来先问了一个系统设计的问题,一个front end, 一个back end. front end有很多UI,一个UI对10个多customers,back end有许多processor,或者pro ...