CodeSpeek

Maximum Depth of Binary Tree

Easy · Trees

You are given the root of a binary tree. Return the number of nodes along the longest path from the root down to any leaf, counting the root itself as depth 1. An empty tree has depth 0.

Examples

Input:  root = [3,9,20,null,null,15,7]
Output: 3
Why:    The longest path from root to a leaf goes through 3 -> 20 -> 15, which has 3 nodes.
Input:  root = [1,null,2]
Output: 2
Why:    The tree is a straight line of 2 nodes from root to the deepest leaf.
Input:  root = []
Output: 0
Why:    An empty tree has no nodes, so its depth is 0.

Constraints

0 <= number of nodes <= 10^4, -100 <= Node.val <= 100

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Maximum Depth of Binary Tree

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Maximum Depth of Binary Tree. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.