Simple O(1) space solution


#1
public class Solution {
    int sum=0;
    public int solve(TreeNode A) {
        int h=height(A);
        int val=Integer.MIN_VALUE;
        for(int i=1;i<=h+1;i++){
            sum=0;
            levelOrder(A,i);
            if(sum>val) val=sum;
        }
    return val;
}
void levelOrder(TreeNode A,int i){
    if(A==null) return;
    if(i==1)    sum+=A.val;
    else if(i>1){
        levelOrder(A.left,i-1);
        levelOrder(A.right,i-1);
    }
}
int height(TreeNode A){
    if(A==null) return -1;
    int lh=height(A.left);
    int rh=height(A.right);
    return Math.max(lh,rh)+1;
}

}