leetcode-cpp 513.找树左下角的值

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;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43255713/article/details/105524541