---
# System prepended metadata

title: LC 513. Find Bottom Left Tree Value
tags: [Binary Tree, medium, leedcode, c++]

---

# LC 513. Find Bottom Left Tree Value

### [Problem link](https://leetcode.com/problems/find-bottom-left-tree-value/)

###### tags: `leedcode` `medium` `c++` `Binary Tree`

Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.

**Example 1:** 
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
```
Input: root = [2,1,3]
Output: 1
```

**Example 2:** 
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
```
Input: root = [1,2,3,4,null,5,6,null,null,7]
Output: 7
```

 **Constraints:** 

- The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
- <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>

## Solution 1 - Iteration
#### C++
```cpp=
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode *> q;
        q.push(root);
        int ans = root->val;
        while (!q.empty()) {
            TreeNode *node = q.front();
            q.pop();
            ans = node->val;
            if (node->right) {
                q.push(node->right);
            }
            if (node->left) {
                q.push(node->left);
            }
        }
        return ans;
    }
};
```

>### Complexity
>w = max width of tree.
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |

## Note
x
