首页 > 编程语言 > 详细

leetcode:Minimum Depth of Binary Tree【Python版】

时间:2014-10-19 10:12:27      阅读:489      评论:0      收藏:0      [点我收藏+]

1、类中递归调用添加self;

2、root为None,返回0

3、root不为None,root左右孩子为None,返回1

4、返回l和r最小深度,l和r初始为极大值;

 1 # Definition for a  binary tree node
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7 
 8 class Solution:
 9     # @param root, a tree node
10     # @return an integer
11     def minDepth(self, root):
12         if root == None:
13             return 0
14         if root.left==None and root.right==None:
15             return 1
16         l,r = 9999,9999
17         if root.left!=None:
18             l = self.minDepth(root.left)
19         if root.right!=None:
20             r = self.minDepth(root.right)
21         if l<r:
22             return 1+l
23         return 1+r

 

leetcode:Minimum Depth of Binary Tree【Python版】

原文:http://www.cnblogs.com/CheeseZH/p/4034307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!