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. 常见浏览器兼容性问题与解决方案css篇

    浏览器兼容问题一:不同浏览器的标签默认的外补丁和内补丁不同 问题症状:随便写几个标签,不加样式控制的情况下,各自的margin 和padding差异较大. 碰到频率:100% 解决方案:CSS里    ...

  2. 怎么设置输入的EditText字母自己主动大写

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/t80t90s/article/details/25048917 复于: 2013-09-06 09: ...

  3. HTML PX/EM换算工具 快捷键

    换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html 快捷键:http://www.runoob.com/tags/html-keyboa ...

  4. abap 基本知识

    sap gui 安装教程:http://www.itpub.net/forum.php?mod=viewthread&tid=2090890 1:abap 基本数据类型: i(整型),f(浮点 ...

  5. ubuntu 安装ftp,配置,和java调用

    1:安装 ftp服务器 sudo apt-get install vsftpd 自动安装使用的是主机的用户名和密码:liyafei,1367xxx 访问方式,ftp://localhost:21,ft ...

  6. UUID生成字符串

    在向数据库插入新数据时,可能需要插入字符串形式的ID,这时使用UUID可以生成随机字符串: String str = UUID.randomUUID().toString();

  7. Java序列化总结

    什么是序列化? 序列化是将对象的状态信息转化为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后可以通过存储区中读取或反序列化对象的状态重新创建对象. 为什么要序 ...

  8. python 的 json 转换

    python 的 json 转换 本文为原创文章,禁止转载! 本文以 json.dumps()  和 json.loads() 方法进行 Python 数据和 json 格式之间转换,进行讲解 首先比 ...

  9. 更高效的MergeSort--稍微优化

    0. 简介 本文简要介绍一下比传统MergeSort更高效的算法,在原来的算法Merge基础上,少发生一半拷贝.欢迎探讨,感谢阅读. 原文链接如下:http://loverszhaokai.com/p ...

  10. Kintone学习

    kintone JavaScript编码指南 编码的注意地方: 文字编码 使用 utf-8