513. Find Bottom Left Tree Value**
https://leetcode.com/problems/find-bottom-left-tree-value/
Title Description
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.
C ++ implementation 1
Sequence through each layer, each time the number of updates leftmost.
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> Queue;
Queue.push(root);
int res = 0;
while (!Queue.empty()) {
int size = Queue.size();
res = Queue.front()->val;
for (int i = size; i > 0; --i) {
auto root = Queue.front();
Queue.pop();
if (root->left)
Queue.push(root->left);
if (root->right)
Queue.push(root->right);
}
}
return res;
}
};