> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/search/level-order-traversal/find-bottom-left-tree-value.md).

# Find Bottom Left Tree Value

<https://leetcode.com/problems/find-bottom-left-tree-value/description/>

> Given a binary tree, find the leftmost value in the last row of the tree.

## Thoughts

要找最后一层最左的元素，那按层遍历到最后一层即可。

## Code

```
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        TreeNode lastLeft = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode newLeft = null;
            int length = queue.size();

            for (int i = 0; i < length; i++) {
                TreeNode node = queue.remove();

                if (node.left != null) {
                    queue.add(node.left);            
                }

                if (node.right != null) {
                    queue.add(node.right);
                }

                if (i == 0) {
                    lastLeft = node;
                }
            }
        }  
        return lastLeft.val;
    }
}
```

## Analysis

时间复杂度O(n).
