# 513. 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

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.

``````class Solution {
public:
int findBottomLeftValue(TreeNode* root) {int max_depth = 1, res = root->val;
helper(root, 1, max_depth, res);
return res;
}
void helper(TreeNode* node, int depth, int& max_depth, int& res) {
if (!node) return;
if (depth > max_depth) {
max_depth = depth;
res = node->val;
}
helper(node->left, depth + 1, max_depth, res);
helper(node->right, depth + 1, max_depth, res);
}
};
``````

``````class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int res = 0;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
int n = q.size();
for (int i = 0; i < n; ++i) {
TreeNode *t = q.front(); q.pop();
if (i == 0) res = t->val;
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return res;
}
};
``````

``````class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q{{root}};
while (!q.empty()) {
root = q.front(); q.pop();
if (root->right) q.push(root->right);
if (root->left) q.push(root->left);
}
return root->val;
}
};
``````

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

https://leetcode.com/problems/find-bottom-left-tree-value/discuss/98779/Right-to-Left-BFS-(Python-%2B-Java)

https://leetcode.com/problems/find-bottom-left-tree-value/discuss/98786/verbose-java-solution-binary-tree-level-order-traversal

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation