/104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree

Easy
Trees78% acceptance

Given the root node of a binary tree, return the maximum depth of the tree. The maximum depth is defined as the length of the longest path from the root node to any leaf node, measured in number of nodes traversed.

Example 1

Input: TreeNode(5, TreeNode(2), TreeNode(8, TreeNode(7), TreeNode(10)))

Output: 3

Explanation: The longest path is 5 -> 8 -> 10, which has 3 nodes.

Example 2

Input: TreeNode(1, TreeNode(2, TreeNode(3)), None)

Output: 3

Explanation: The longest path is 1 -> 2 -> 3, which has 3 nodes.

Constraints

  • The number of nodes in the binary tree is in the range [0, 10_000].
  • Each node's value is an integer in the range [-100, 100].
Python (current runtime)

Case 1

Input: TreeNode(4, TreeNode(1), TreeNode(6, None, TreeNode(9)))

Expected: 3

Case 2

Input: TreeNode(11)

Expected: 1

Case 3

Input: None

Expected: 0

Case 4

Input: TreeNode(7, TreeNode(3, TreeNode(1)), TreeNode(8))

Expected: 3