topic:
Given a binary tree, find the leftmost value in the last row of the tree.
Example:
Input:
2
/\
1 3
Output:
1
solution
The topic requires [find the last line], [the value of the leftmost node]
think of the layer sequence traversal.
Each time the layer sequence is traversed, record the leftmost value.
Code
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
if(root) que.push(root);
int result = 0;
while(!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
if (i == 0) result = node->val;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};