Skip to main content

104 - Maximum Depth of Binary Tree

Details

KeyValue
Linkhttps://leetcode.com/problems/maximum-depth-of-binary-tree/
LanguagePython 3
Runtime55 ms, faster than 71.41% of Python3 online submissions for Maximum Depth of Binary Tree
Memory Usage16.6 MB, less than 23.50% of Python3 online submissions for Maximum Depth of Binary Tree
DatastructuresTreeNode
AlgorithmsDFS
ComplexityTime: O(N) Memory: O(logN)

Procedure

  1. ...

Code

Option 1: Terse

# 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: Optional[TreeNode]) -> int:
if not root: return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Option 2: Verbose

# 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: Optional[TreeNode]) -> int:
depth = 1
def dfs(node):
nonlocal depth
if not node: return 0
if not node.left and not node.right: return 0
depth += 1
dfs(node.left)
dfs(node.right)

dfs(root)
return depth