111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree

[Easy] “Given a binary tree, find its minimum depth."

Link to Leetcode

Note: old solution, should review (TODO:)

Python3:

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

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        
        def search_node(root, count=0):
            if not root:
                return count
            else:
                count += 1
    
            if root.left and not root.right:
                return search_node(root.left, count)
            elif root.right and not root.left:
                return search_node(root.right, count)
            else:
                return min(search_node(root.left, count), search_node(root.right, count)) 
        
        return search_node(root)