Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Maximum Depth of Binary Tree

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Maximum Depth of Binary Tree. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

At Dropbox, a service stores hierarchical folder metadata as a binary tree. Given the root of a binary tree, return its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Formal Specification

  • Input: root, the root node of a binary tree, or null for an empty tree.
  • Output: An integer representing the maximum depth of the tree.

You should solve this as a standard binary tree traversal problem.

Constraints

  • The number of nodes in the tree is in the range [0, 10^4]
  • -100 <= Node.val <= 100
  • The tree may be empty
  • Each node has attributes val, left, and right

Function Signature

def max_depth(root):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output