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. 【PHP】PHP初学者的学习线路

    先来看下PHP初学者的学习线路: (1) 熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作简单的网页,对元素属性相对熟悉. (2) 理解动态语言的概念和运做机制,熟悉基本的PHP语法. ( ...

  2. Web Responsive Table, 只需CSS使table在手机和平板中完美显示

    在做responsive或者手机版页面的时候,经常碰到<Table>在手机和平板中会因为长度问题把页面撑大.最近看到一个比较好,比较方便的方法,而且仅仅用CSS 2就可以实现! 实例URL ...

  3. [vue]计算和侦听属性(computed&watch)

    先看一下计算属性 vue只有data区的数据才具备响应式的功能. 计算和侦听属性 - v-text里可以写一些逻辑 <div id="example"> {{ mess ...

  4. MVC中Controller与View之间的数据传递

    一.Controller向View传递数据 Controller向View传递数据有3种形式: 通过ViewData传递 在Controller里面的Action方法中定义ViewData,并且赋值, ...

  5. [转]Visual C++ RunTime的特征——非烫即屯

    Visual C++ RunTime的特征——非烫即屯 大一刚学C语言,第二次上机课,当我发现我照着书抄写的程序在运行之外的黑框里面跳出一排“烫烫烫烫烫”,当时就震惊了.你们能想象一个来自小城, 在大 ...

  6. 20155239 2016-2017-2 《Java程序设计》第5周学习总结

    教材内容学习 第八章 JAVA异常架构 Java异常是Java提供的一种识别及响应错误的一致性机制. Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅,并提高程序健壮性 ...

  7. Linux服务器---安装jdk

    安装jdk jdk是运行或者开发java的必须工具,很多软件都会依赖jdk,因此必须学会安装jdk 1.查看当前系统的jdk情况 [root@localhost wj]# rpm -qa | grep ...

  8. 在notepad++里面使用正则表达式替换掉所有行逗号前面内容

    需求:在notepad++里面使用正则表达式替换掉所有行逗号前面内容,一文本内容如下(只贴一小部分,实际上N多): 级别,層級程序,程式插件,外掛程式鼠标,滑鼠打印,列印打开,開啟博客,部落格联系,聯 ...

  9. MySQL-MHA集群部署(binlog复制)

    MHA的理论知识网上有很多教程,这里不会说明:仅推荐博客链接! MHA的理论说明:http://www.ywnds.com/?p=8094 MHA的安装包需要在google上面下载,或者就是csdn上 ...

  10. Arduino使用HC05蓝牙模块与手机连接

    Arduino使用HC05蓝牙模块与手机连接 一切都是最好的选择 首先是线路连接,一定不要接错了 Arduino 代码 #include <SoftwareSerial.h> // Pin ...