This problem defines a 1-TreeNode tree to have height of 1.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
- Time Complexity: O(n) since we must touch all nodes
- Space Complexity: O(n) due to recursion (on a tree that may not be balanced)