Leetcode Solutions: Maximum Depth of Binary Tree
SalahElhossiny
Posted on August 25, 2022
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
class Solution(object):
def maxDepth(self, root):
stack = [[root, 0]]
res = 0
while stack:
node, depth = stack.pop()
res = max(res, depth)
if node:
stack.append([node.left, depth + 1])
stack.append([node.right, depth + 1])
return res
💖 💪 🙅 🚩
SalahElhossiny
Posted on August 25, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.