Top 150 InterviewЖесткий

Maximum Depth of Binary Tree

Detailed guide and Python implementation for the 'Maximum Depth of Binary Tree' problem.

Постановка задачи

Жесткий

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

The tree is represented as a level-order list where None represents a missing node. Implement a function maxDepth(root: list) -> int.

Ограничения
  • The number of nodes in the tree is in the range [0, 10000]
  • -100 <= Node.val <= 100

Примеры

Example 1
Input
[3,9,20,None,None,15,7]
Output
3
Explanation

The tree has 3 levels: root [3], second level [9,20], third level [15,7]. The longest path is 3->20->15 or 3->20->7, both of depth 3.

Example 2
Input
[1,None,2]
Output
2
Explanation

The tree has root 1 with only a right child 2. Depth is 2.

Example 3
Input
[]
Output
0
Explanation

An empty tree has depth 0.

Need a Hint?
Consider using Trees-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Готовы решить?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Открыть в редакторе
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Рекомендуемые ресурсы Python

Расширьте свои знания с помощью соответствующих интерактивных руководств, шпаргалок и сравнений кода.

Учебник по Python

Циклы Python

Узнайте, как использовать циклы Python для перебора данных. Освойте циклы for, while, прерывание, продолжение и лучшие практики работы с циклами с помощью интерактивных примеров.

Посмотреть ресурс
Практическое руководство

Как найти длину списка в Python

Узнайте, как найти длину списка в Python с помощью функции len(). Поймите временную сложность O(1) и количество проверок.

Посмотреть ресурс
Шпаргалка

Шпаргалка по строковым методам Python

Полное справочное руководство по манипулированию строками в Python. Мастер форматирования, поиска, разделения, замены и проверки свойств строк.

Посмотреть ресурс
Сравнение языков

Python против JavaScript: какой язык программирования лучше?

Всестороннее сравнение Python и JavaScript. Изучите синтаксические различия, производительность, варианты использования (серверная и клиентская части) и примеры кодирования.

Посмотреть ресурс