The error is NullPointException??? I need help please.


  • 1
    M

    public class Solution {
    public int maxDepth(TreeNode root) {
    if(root==null) return 0;
    int max=1;
    return dfs(root,max);
    }
    int dfs(TreeNode root,int max){
    if(root.left==null&&root.right==null)return max;
    if(root.left==null&&root.right!=null)return dfs(root.right,max+1);
    if(root.left!=null&&root.right==null)return dfs(root.right,max+1);
    return Math.max(dfs(root.left,max+1),dfs(root.right,max+1));
    }
    }


  • 2
    L

    Please at least do some code formatting.


Log in to reply
 

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