Leetcode 111. Minimum depth of binary tree | Python

Time:2020-10-24

111. Minimum depth of binary tree


Source: leetcode https://leetcode-cn.com/problems/minimum-depth-of-binary-tree

subject


Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node.

Note: leaf node refers to the node without child nodes.

Example:

Given the binary tree [3,9,20, null, null, 15,7],

    3
   / \
  9  20
    /  \
   15   7

Returns its minimum depth of 2

Thinking of problem solving


Idea: DFS, BFS

The minimum depth of the given binary tree is required.

Minimum depth:It refers to the number of nodes on the shortest path from the root node to the leaf node.

Leaf node:A node that has no left or right children.

Then we consider starting from the root node, using DFS, BFS ideas to search.

DFS

Let’s first look at the method of using DFS (depth first search) as follows:

  • If the root node is empty, 0 is returned;
  • If the root node is not empty, you need to judge the left and right child nodes:

    • If the left and right child nodes are empty, return 1;
    • If one of the left and right child nodes is empty, the minimum depth of non empty child nodes is returned;
    • If the left and right child nodes are not empty, the value of the smaller depth is returned.

For specific code, see code implementation DFS

BFS

Here, we can also use the BFS (breadth first search) method. When we use the BFS method, we search layer by layer. If we find that there is no child node in a certain layer, it means that the shortest path from the root node to the current node is.

For specific code, see code implementation BFS

code implementation


# DFS
# 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:
        #The root node is empty
        if not root:
            return 0
        #If the root node is not empty, discuss the left and right child nodes respectively
        depth = 1

        #The root node is not empty, but there are no left and right child nodes. Return 1
        if not root.left and not root.right:
            return 1
        #If there is no right child node, the minimum depth of left child node that is not empty is returned
        elif not root.right:
            depth += self.minDepth(root.left)
        #Returns the minimum depth of a non empty right child node
        elif not root.left:
            depth += self.minDepth(root.right)
        #Left and right child nodes are not empty, return a smaller depth
        else:
            left_depth = self.minDepth(root.left)
            right_depth = self.minDepth(root.right)
            depth += min(left_depth, right_depth)

        return depth

# BFS
# 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:
        if not root:
            return 0

        from collections import deque
        queue = deque()
        queue.append(root)

        depth = 1

        while queue:
            #Search layer by layer, first record the number of nodes in each layer
            size = len(queue)
            for _ in range(size):
                node = queue.popleft()
                #If there is no child node in the current node, then the current node is on the minimum path
                if not node.left and not node.right:
                    return depth
                #If one of the nodes is null, the minimum depth of the non empty node is returned
                elif not node.right:
                    queue.append(node.left)
                elif not node.left:
                    queue.append(node.right)
                #Left and right child nodes will not be empty, return the smaller depth
                else:
                    queue.append(node.left)
                    queue.append(node.right)
                
            depth += 1

Achieving results


Leetcode 111. Minimum depth of binary tree | Python

Leetcode 111. Minimum depth of binary tree | Python

Welcome to our attention


The official account [book collection]

Recommended Today

Java security framework

The article is mainly divided into three parts1. The architecture and core components of spring security are as follows: (1) authentication; (2) authority interception; (3) database management; (4) authority caching; (5) custom decision making; and;2. To build and use the environment, the current popular spring boot is used to build the environment, and the actual […]