The Maximum Depth of Binary Tree solutions are below.
15 Jun 2024•Easy•C++ / Python / TypeScript
Maximum Depth of Binary Tree
C++, Python and TypeScript solutions for Maximum Depth of Binary Tree.
Solution files
Python•maximum-depth-of-binary-tree/synced-solution.py
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
C++•maximum-depth-of-binary-tree/synced-solution.cpp
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
TypeScript•maximum-depth-of-binary-tree/synced-solution.ts
function maxDepth(root: TreeNode | null): number {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}