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.

The maximum depth of the binary tree in the above image is 3.
Solution
Section titled “Solution”Use a recursive function to calculate the maximum depth of the tree. The function will return the maximum depth of the left and right child nodes, and then return the maximum of the two plus one.
Implementation
Section titled “Implementation”# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = right
class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int """
if root is None: return 0
return max(self.maxDepth(root.right), self.maxDepth(root.left)) + 1Pseudocode
Section titled “Pseudocode”- If the root is
None, return 0. - Return the maximum of the maximum depth of the left and right child nodes plus one.
Complexity Analysis
Section titled “Complexity Analysis”- The time complexity of this algorithm is O(N), where N is the number of nodes in the binary tree.
- The space complexity of this algorithm is O(N), where N is the number of nodes in the binary tree.