513. Find the value of the lower left corner of the tree
-
link
leetcode
-
solution:
The first value of the last layer of BFS
-
code
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int res=0;
queue<TreeNode* >q;
q.push(root);
while(!q.empty()){
int len=q.size();
TreeNode* m=q.front();
res=m->val;
while(len--){
TreeNode* t=q.front();
q.pop();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};