513.找树左下角的值
-
链接
leetcode
-
solution:
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;
}
};