Skip to content
BASA DSA

Maximum Depth of Binary Tree

EasyTrees

Return the maximum number of nodes on a root-to-leaf path. An empty binary tree has depth zero.

Example 1

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 1, node 1: 2, node 2: 3. Edges: node 0 (1) — node 1 (2) right child; node 1 (2) — node 2 (3) right child.123
Argument 1 tree: level-order input; omitted children are null.

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

Explanation

The right-only chain 1->2->3 contains three nodes on its deepest path.

Example 2

Argument 1 tree: empty tree (null root).
Input
root = []
Output
0

Explanation

An empty tree has depth zero.

Example 3

Argument 1 tree: level-order input; omitted children are null.Argument 1 tree: level-order input; omitted children are null. Vertices: node 0: 7, node 1: 8, node 2: 9. Edges: node 0 (7) — node 1 (8) left child; node 0 (7) — node 2 (9) right child.789
Argument 1 tree: level-order input; omitted children are null.

Input
root = [7,8,9]
Output
2

Explanation

The root and either child give depth two.

Constraints

  • 0 <= number of nodes <= 10000
  • Trees are breadth-first arrays with null holes

Function contractmaxDepthArguments: tree
Loading draft…
SolutionRuns on your device
Loading draft…