A simple C solution


  • 0
    B
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    int result;
    void preorder(struct TreeNode* root,int flag)
    {
        if(root==NULL) return;
        if(flag==0&&root->left==NULL&&root->right==NULL)
            result=result+root->val;
        preorder(root->left,0);
        preorder(root->right,1);
    }
    int sumOfLeftLeaves(struct TreeNode* root) {
        if(root==NULL) return 0;
        result=0;
        preorder(root->left,0);
        preorder(root->right,1);
        return result;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.