Source of the topic
https://leetcode.com/problems/minimum-depth-of-binary-tree/
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along, the shortest path from the root node to the nearest leaf node.
Test instructions Analysis
Input:a binary Tree
Minimum depth of output:leaf node
Conditions: Note that it must be the leaf node to find the minimum depth of the leaf node
Topic ideas
Recursive, note differs from seeking maximum depth
AC Code (PYTHON)
1 #Definition for a binary tree node.2 #class TreeNode (object):3 #def __init__ (self, x):4 #self.val = x5 #self.left = None6 #self.right = None7 8 classsolution (object):9 defmindepth (self, root):Ten """ One : Type Root:treenode A : Rtype:int - """ - ifRoot = =None: the return0 - ifRoot.left = = None andRoot.right! =None: - returnSelf.mindepth (root.right) + 1 - ifRoot.left! = None andRoot.right = =None: + returnSelf.mindepth (root.left) + 1 - returnMin (self.mindepth (root.left), self.mindepth (root.right)) + 1 +
[Leetcode] (python): 111 Minimum Depth of Binary Tree