Skip to main content

Maximum Depth of Binary Tree

Question

com

What is the maximum depth of a given binary tree?

Example 1
Input: [3,9,20,null,null,15,7]

Output: 3

Solution

all//Maximum Depth of Binary Tree.py


# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def maxDepth(self, root):
if not root:
return 0

left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)

return max(left_depth, right_depth) + 1