104. Maximum Depth of Binary Tree ¶ Time: $O(n)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8class Solution { public: int maxDepth(TreeNode* root) { if (root == nullptr) return 0; return 1 + max(maxDepth(root->left), maxDepth(root->right)); } }; 1 2 3 4 5 6 7class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } } 1 2 3 4 5class Solution: def maxDepth(self, root: TreeNode | None) -> int: if not root: return 0 return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))