Skip to content
LC-0104 Easy LeetCode

104. Maximum Depth of Binary Tree

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 77% Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(h), h is height of binary tree

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):
        if root is None:
            return 0
        else:
            return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Solution from kamyu104/LeetCode-Solutions · MIT