The purpose of this problem is to find the leftmost value in the last row of the tree, what first come to me is BFS. Using a queue for auxiliary, we traverse the tree row by row, finding the leftmost value of each row and update the returned value. Until the queue is empty, which means the whole tree has been visited, the last value is the leftmost value of the bottom row. The whole process is almost like that in BFS.
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(root==NULL)
return 0;
int Lvalue = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode* TNode = q.front();
q.pop();
if(i==0) Lvalue = TNode->val;
if(TNode->left) q.push(TNode->left);
if(TNode->right) q.push(TNode->right);
}
}
return Lvalue;
}
};