Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Maximum Depth (or Height) of Binary Tree (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a root node of the binary tree, return the depth (or height) of a binary tree.

The Depth of the binary tree refers to the number of nodes along the longest path from the root node to the farthest leaf node. If the tree is empty, the depth is 0.

Examples

Example 1

  • Input: root = [1, 2, 3, 4, 5]
Image
  • Expected Output: 3
  • Explanation: The longest path is 1->2->4 or 1->2->5 with 3 nodes.

Example 2

  • Input: root = [1, null, 2, null, 3]
Image
  • Expected Output: 3
  • Justification: There's only one path 1->2->3 with 3 nodes.

Example 3

  • Input: root = [1, 2, 3, 4, 7, null, null, null, null, null, 9]
Image
  • Expected Output: 4
  • Justification: The longest path is 1->2->7->9 with 4 nodes.

Constraints:

  • The number of nodes in the tree is in the range [0, 10<sup>4</sup>].
  • -100 <= Node.val <= 100

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible