[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




Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Autonomous Teams, Part 1: Do They Scale?

Wealth Wise 1.2 Release

RespiraWorks is Merging with BostonVent!

BostonVent Ventilator and GUI Prototype Images

The Names of Names

Pointers in C

Create a Blog with Django

CSS: Day 22

An illustration of the not pseudo class selector

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
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

LeetCode Patterns Adventure 17 — Binary Search

Hackerrank — Sherlock and the Valid String walkthrough #Python #Hackerrank

Introduction to LinkedList

Sorting Algorithms Explained Using Python: Merge Sort

Glass Cube and Dots