8 seconds c solution


  • 0
    A

    typedef struct dpr {
    int r_max;
    int r_next_level_max;
    }dpr_t;

    int max(int a, int b)
    {

    return a > b ? a : b;
    

    }

    dpr_t getmax(struct TreeNode *root)

    {

    dpr_t dt = {0, 0};
    if (!root) {
        return dt;
    }
    dpr_t dt_l = getmax(root->left);
    dpr_t dt_r = getmax(root->right);
    
    dt.r_max = max(root->val + dt_l.r_next_level_max +dt_r.r_next_level_max,
                   dt_l.r_max + dt_r.r_max);
    dt.r_next_level_max = dt_l.r_max + dt_r.r_max;
    return dt;
    

    }

    int rob(struct TreeNode* root) {

    dpr_t dt = getmax(root);
    return max(dt.r_max, dt.r_next_level_max);
    

    }


Log in to reply
 

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