ntree.ai
ntree.aiMaximum Depth of Binary Tree

Maximum Depth of Binary Tree

easysolvedattempted

You are given the root of a binary tree.

Return its maximum depth, the number of nodes along the longest path from the root down to a leaf.

Example 1:

3
9
11
8
20
15
7
Input:  root = [3, 9, 20, 11, null, 15, 7, 8]
Output: 4

Example 2:

1
2
Input:  root = [1, null, 2]
Output: 2

Constraints:

  • 0 <= number of nodes <= 10^4
  • -100 <= Node.val <= 100
# Definitions:
# class TreeNode:
# def __init__(self, val=0, left=None, right=None, parent=None):
# self.val = val
# self.left = left
# self.right = right
# self.parent = parent
 
def maxDepth(root: TreeNode) -> int:
pass
 

Press Visualise to step through your code.