No Stacks No Queue | Simple preorder and array


#1
public class Solution {
    ArrayList<Integer> res;
    public int solve(TreeNode A) {
        res = new ArrayList<Integer>();
        
        preorder(A,0);//O(n) time and space
        int max=Integer.MIN_VALUE;
        
        for(int sum : res)  max = sum > max ? sum : max;
        
        return max;
    }
    
    
    public void preorder(TreeNode node,int row){
        if(node == null)    return;
        
        if(res.size() <= row)   res.add(0);
        res.set(row,res.get(row)+node.val);
        
        preorder(node.left,row+1);
        preorder(node.right,row+1);
    }
}