Untitled
public int maxDepth(TreeNode root) { if (root == null) return 0; // Base case: Empty tree int leftDepth = maxDepth(root.left); // Depth of left subtree int rightDepth = maxDepth(root.right); // Depth of right subtree return 1 + Math.max(leftDepth, rightDepth); // Add 1 for the current node }
Leave a Comment