Skip to content

513. Find Bottom Left Tree Value

难度: Middle

刷题内容

原题连接

  • https://leetcode-cn.com/problems/find-bottom-left-tree-value/

内容描述

``` 给定一个二叉树,在树的最后一行找到最左边的值。

示例 1:

输入:

2

/ \ 1 3

输出: 1

示例 2:

输入:

    1
   / \
  2   3
 /   / \
4   5   6
   /
  7

输出: 7

注意: 您可以假设树(即给定的根节点)不为 NULL。 ```

解题方案

思路 1

从左到右层次遍历二叉树
 int findBottomLeftValue(TreeNode* root) {
    vector<TreeNode*> que;
    que.push_back(root);
    while(que.size()>0){
        vector<TreeNode*> tmp;
        int len = que.size();
        for(int i=0;i<len;i++){
            if(que[i]->left)
                tmp.push_back(que[i]->left);
            if(que[i]->right)
                tmp.push_back(que[i]->right);
        }
        if(tmp.size()==0)
            break;
        que = tmp;
    }
    return que[0]->val;
}

我们一直在努力

apachecn/AiLearning

【布客】中文翻译组