Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Height / Max Depth of Binary Tree

Medium Acceptance 75.36% Points 30.00

You are given the root of a binary tree. Your task is to find and return the maximum depth of the tree. The maximum depth is the number of nodes along the longest path from the root to a leaf.

Examples
Example 1

image Input: root = [3,9,20,null,null,15,7]
Output: 3
Explanation: The longest root-to-leaf path contains 3 nodes.

Example 2

image Input: root = [1,null,2]
Output: 2
Explanation: The tree has two levels.

Example 3

image Input: root = []
Output: 0
Explanation: The tree is empty.

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
Samsung Media.net
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions