0
点赞
收藏
分享

微信扫一扫

【宽搜】6. leetcode 513 找树左下角的值

1 题目描述

题目链接:找树左下角的值
在这里插入图片描述

2 题目解析

思路:

3 代码

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if (root == nullptr)
            return 0;

        int res = 0;
        queue<TreeNode*> q;
        q.push(root);

        while(q.size())
        {
            vector<int> tmp;
            int sz = q.size();
            for (int i = 0; i < sz; ++ i)
            {
                TreeNode* t = q.front();
                q.pop();

                tmp.push_back(t->val);

                if (t->left)
                    q.push(t->left);
                if (t->right)
                    q.push(t->right);
            }

            //当这一层为最后一层的时候
            if (q.size() == 0)
                res =  tmp[0];
        }

        return res;
    }
};

在这里插入图片描述

举报

相关推荐

0 条评论