Leetcode Solutions: Maximum Depth of Binary Tree

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


Enter fullscreen modeExit fullscreen mode



Source link

Comments 0

Leave a Reply

Your email address will not be published. Required fields are marked *