def max_depth(root):
if not root: return 0
return 1+max(max_depth(root.left),max_depth(root.right))
public int maxDepth(TreeNode root){
if(root==null) return 0;
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
'not root' works — None is falsy. max() vs Math.max().
def max_depth(root):
if not root: return 0
return 1+max(max_depth(root.left),max_depth(root.right))
public int maxDepth(TreeNode root){
if(root==null) return 0;
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
1. Null: return 0 2. Return 1+max(depths)