513. Find Bottom Left Tree Value (Medium)

原题目:
  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.

题目大意如下:
  找出给定二叉树最后一排的最左边的那个节点。

解题思路:
  类似于层序遍历,分别定义一个up和down的vector,分别存储上一排和下一排的结点,当下一排节点个数为零时,就说明up此时储存的是最后一排,此时返回up[0]就可以了。

代码如下:

/**
 * 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) {
        vector<TreeNode*> up ,down ;
        up.push_back(root) ;
        while(1){
            for(auto i : up){
                if(i->left != NULL) down.push_back(i->left) ;
                if(i->right != NULL) down.push_back(i->right) ;
            }
            if(!down.empty()){
                up = down ;
                down.clear() ;
            }
            else return up[0]->val ;
        }

    }
};

运行结果:
运行结果

知识补充:
  关于基于范围的for循环以及auto关键字的使用:
C++11支持(range-based)的 for循环。在有的时候显得很方便,可以节省一些代码量。

可以遍历的对象包括:

数组。(不包括指针)
定义了begin()和end()方法,且返回该方法返回迭代器的类对象。(STL 中所有容器都可以)
说白了这个特性就是基于迭代器实现的。

模板:

 for ( range_declaration : range_expression) loop_statement

样例:

for(auto i : up){
    if(i->left != NULL) down.push_back(i->left) ;
    if(i->right != NULL) down.push_back(i->right) ;
 }

C++11中引入的auto主要有两种用途:自动类型推断和返回值占位。auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。
1. 自动类型推断
  auto自动类型推断,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推断,可以大大简化我们的编程工作。下面是一些使用auto的例子。

#include <vector>
#include <map>

using namespace std;

int main(int argc, char *argv[], char *env[])
{
//  auto a;                 // 错误,没有初始化表达式,无法推断出a的类型
//  auto int a = 10;        // 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。

    // 1. 自动帮助推导类型
    auto a = 10;
    auto c = 'A';
    auto s("hello");

    // 2. 类型冗长
    map<int, map<int,int> > map_;
    map<int, map<int,int>>::const_iterator itr1 = map_.begin();
    const auto itr2 = map_.begin();
    auto ptr = []()
    {
        std::cout << "hello world" << std::endl;
    };

    return 0;
};

// 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,
// 不使用auto将很难确定变量的类型(使用auto后,将由编译器自动进行确定)。
template <class T, class U>
void Multiply(T t, U u)
{
    auto v = t * u;
}

2, 返回值占位

template <typename T1, typename T2>
auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)
{
   return t1+t2;
}
auto v = compose(2, 3.14); // v's type is double

注意事项:
1.用auto声明的变量必须初始化;
2.函数和模板参数不能被声明为auto;
3.因为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值