Max Depth of Binary Tree

MediumTree Traversal

Description

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. The tree is represented as a nested object: `{ val, left, right }` where `left` and `right` can be `null`.

Examples

Input: root = { val:3, left:{val:9,left:null,right:null}, right:{val:20,left:{val:15,left:null,right:null},right:{val:7,left:null,right:null}} }

Output: 3

Input: root = { val:1, left:null, right:{val:2,left:null,right:null} }

Output: 2

Constraints

  • •Number of nodes: 0 to 10⁴
  • •-100 ≤ Node.val ≤ 100

Your Solution

JavaScript·4 test cases·Max 5,000 chars · 2s timeout · 10 runs/30s
Loading...