Question: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
Question Name: Minimum Depth of Binary Tree
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return an integer def minDepth(self, root): if root == None: # This is an empty tree. return 0 elif root.left == None and root.right == None: # This is a leaf. return 1 elif root.left == None: # This non-leaf node only has right son. return 1 + self.minDepth(root.right) elif root.right == None: # This non-leaf node only has left son. return 1 + self.minDepth(root.left) else: # This non-leaf node has both left son and right son. return 1 + min(self.minDepth(root.left), self.minDepth(root.right)) |