Untitled
Anonymous
plain_text
12/29/2024 4:51 PM
444 B
17
Indexable
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
}Editor is loading...
Leave a Comment