请教下leetcode 404 求树的所有左叶子的值的和

我的思路就是很简单的,广度遍历,也就是俗话说的一层一层遍历二叉树,碰到有左叶子的就把左叶子的值push_back进vector,感觉一点问题也没有啊。。不知道为什么就是少值,请教下巨巨们 。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    private:
    vector<int> a;
    queue<TreeNode *> b;

public:

    int sumOfLeftLeaves(TreeNode* root) {

        if(!root)
            return 0;

        b.push(root);

        while(!b.empty())
        {
            if(b.front()->right&&b.front()->left)
            {
                a.push_back(b.front()->left->val);
                b.push(b.front()->left);
                b.push(b.front()->right);
                b.pop();
            }
            else if(!b.front()->left&&!b.front()->right)
            {
                b.pop();
            }

            else
            {
                if(b.front()->left)
                {
                    a.push_back(b.front()->left->val);
                    b.push(b.front()->left);
                    b.pop();
                }
                else if(b.front()->right)
                {

                    b.push(b.front()->right);
                    b.pop();
                }
            }

        }

        int sum = 0;

        for(int i = 0;i<a.size();i++)
        {
            sum+=a[i];
        }

        return sum;
    }
};

https://blog.csdn.net/weixin_42130471/article/details/80315983

你这个递归将所有左侧的节点的值都加起来的,叶节点指的是最底层的节点

/*
例如
      1
    /  \
   2  5
  /\
3 4 
*/ 
//这种情况下,你求出来的是2+3,而实际应得3