Maximum Depth of Binary Tree

Maximum Depth of Binary Tree is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Tree Traversal Patterns (DFS & BFS) pattern, in the Recursive Postorder Traversal subpattern.

Problem statement

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.

Example 1:

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

Example 2:

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

Constraints:

- The number of nodes in the tree is in the range [0, 104].

- -100 <= Node.val <= 100

Topics and companies

Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree.

Reported in interviews at LinkedIn, Amazon, Intel.

How to practise Maximum Depth of Binary Tree on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(h) space.

Open Maximum Depth of Binary Tree in the code editor, or read the Maximum Depth of Binary Tree editorial for a worked solution with its approach and complexity analysis.

Where Maximum Depth of Binary Tree sits in the DSA pattern sheet

Problems related to Maximum Depth of Binary Tree

Other problems that use the same Recursive Postorder Traversal and Tree Traversal Patterns (DFS & BFS) techniques:

Preparing your workspace

Progress is saved as you study.