Max Depth of Binary Tree

Leetcode link: https://leetcode.com/problems/maximum-depth-of-binary-tree/

Question#

Given the root of a binary tree, find the maximum depth.

Example

Input: root = [3,9,20,null,null,15,7]
Output: 3

Implementation#

We can do a depth first search to find the depth.

def maxDepth(self, root: TreeNode) -> int:
if(root is None):
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))