Maximum Depth of a Binary Tree
Problem statement
Return the maximum number of nodes on a path from the root to a leaf. An empty tree has depth0 and a single node has depth1. Node values do not affect depth.
Function
maxDepth(root: TreeNode) → intExamples
Example 1
root = [3,1,5,null,2,4,6]return = 3Every longest root-to-leaf path includes3nodes, such as3 → 1 → 2.
Example 2
root = []return = 0There is no node in the empty tree.
Constraints
0 <= serialized tree length <= 400.-1000 <= every non-null node value <= 1000.- The input is a valid acyclic binary tree serialized level by level, with null for a missing child; [] denotes an empty tree.