My Java Solution using recursive


  • 0
    F
    public int maxDepth(TreeNode root) {
    	if (root == null)
    		return 0;
    
    	int depth = 1, depL = 0, depR = 0;
    	if (root.left != null) {
    		depL = maxDepth(root.left);
    	}
    	if (root.right != null) {
    		depR = maxDepth(root.right);
    	}
    	depth += depL > depR ? depL : depR;
    	return depth;
    }

Log in to reply
 

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