Simple ~15 lines solution using recusrion


#1
int maxi =0;
void sol(TreeNode* A,vector<int> &k,int i){
    k[i]+=A->val;
    if(A->left)sol(A->left,k,i+1);
    if(A->right)sol(A->right,k,i+1);
    maxi = max(maxi,i+1);
    return;
}

int Solution::solve(TreeNode* A) {
    vector<int> k(1000,0);
    sol(A,k,0);
    int i = INT_MIN;
    for(int j = 0;j<maxi;j++) if(i<k[j])i=k[j];
    return i;
}