Leetcode Solutions: Maximum Depth of Binary Tree

salahelhossiny

SalahElhossiny

Posted on August 25, 2022

Leetcode Solutions: Maximum Depth of Binary Tree

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


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
salahelhossiny
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.

Related

Distribute Candies
python Distribute Candies

May 13, 2023

Third Maximum Number
python Third Maximum Number

August 30, 2022

Max Rotate Function
python Max Rotate Function

August 23, 2022

Number of Islands
python Number of Islands

August 23, 2022