Loading...
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.
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.
Node.val ≤1003 is the root, 9 and 20 are its children, and 15 and 7 are the children of 20. The longest path has 3 nodes.
Node 1 has only a right child, 2.
The empty tree has depth 0.
Click "Run" to test with sample cases or "Submit" to run all tests.