15 Jun 2024C++ / Python / TypeScriptEasy

Maximum Depth of Binary Tree

Collected C++, Python, TypeScript solutions for maximum depth of binary tree. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Maximum Depth of Binary Tree. the solution files are available below.

solution files

  • C++ maximum-depth-of-binary-tree/synced-solution.cpp
  • Python maximum-depth-of-binary-tree/synced-solution.py
  • TypeScript maximum-depth-of-binary-tree/synced-solution.ts

Solution files

Pythonmaximum-depth-of-binary-tree/synced-solution.py
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return class="syntax-number">0
        return class="syntax-number">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 class="syntax-number">0;
        return class="syntax-number">1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};
TypeScriptmaximum-depth-of-binary-tree/synced-solution.ts
function maxDepth(root: TreeNode | null): number {
    if (!root) return class="syntax-number">0;
    return class="syntax-number">1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}