[LeetCode]#104. Maximum Depth of Binary Tree

Input: root = [3,9,20,null,null,15,7]
Output: 3
  1. If root is None, return 0.
  2. If roo.left and root.right are None, return 1.
  3. Calculate 1+max (root.left maxdepth , root.right maxdepth)
class Solution:
def maxDepth(self, root):
if root is None:
return 0
if root.left is None and root.right is None:
return 1
ans= 1 + max(self.maxDepth(root.left),self.maxDepth(root.right))
return ans

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store