llllmz

导航

404. 左叶子之和c

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
void postorder(struct TreeNode* root,int* sum,int tag){
    if(!root) return;
    postorder(root->left,sum,1);
    postorder(root->right,sum,0);
    if(tag==1 && !root->left&&!root->right) *sum+=root->val;
}

int sumOfLeftLeaves(struct TreeNode* root){
    if(!root) return 0;
    int sum=0;
    postorder(root,&sum,3);
    return sum;
}

结果;

posted on 2024-03-05 21:38  神奇的萝卜丝  阅读(11)  评论(0)    收藏  举报