839. Height / Maximum Depth of Binary Tree

EasyTreesBinary TreeDFSRecursion

Given the root of a binary tree, return its maximum depth — the number of nodes along the longest path from the root down to the farthest leaf. An empty tree has depth 0. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the maximum depth.

Examples

Example 1
Input: [1,2,3,4,5,null,6]
Output: 3
Explanation: Longest root-to-leaf path has 3 nodes.
Example 2
Input: [1]
Output: 1
Explanation: Single node.
Example 3
Input: []
Output: 0
Explanation: Empty tree.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →