Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Tree Encoding
Your function receives root as a TreeNode (or the empty tree, as null); the node type is provided for you, with val, left and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
Constraints
The number of nodes in the tree is in the range [0,104].
−100≤Node.val≤100
Examples
Example 1
Input
root = [3, 9, 20, null, null, 15, 7]
root
Output
3
Explanation
3 is the root, 9 and 20 are its children, and 15 and 7 are the children of 20. The longest path has 3 nodes.
Example 2
Input
root = [1, null, 2]
root
Output
2
Explanation
Node 1 has only a right child, 2.
Example 3
Input
root = []
Output
0
Explanation
The empty tree has depth 0.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up