leetcode+ 二叉树所有左子叶点sum,递归

点击打开链接
class Solution {
public:
    int sum =0;
    int sumOfLeftLeaves(TreeNode* root) {
         if (root == NULL) return 0;
        if (root->left) {
            if (root->left->left == NULL && root->left->right == NULL) {
                return root->left->val + sumOfLeftLeaves(root->right);
            } else {
                return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
            }
        } else {
            return sumOfLeftLeaves(root->right);
        }
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/81054681
今日推荐