题解 | #二叉树的最小深度#

二叉树的最小深度

http://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

#
# 
# @param root TreeNode类 
# @return int整型
#
import sys
sys.setrecursionlimit(1000000)  # 提高递归深度

class Solution:
    def run(self , root ):
        # write code here
        def do_tree(cur):
            if cur is None:
                return 0
            if cur.left is None and cur.right is None:
                return 1
            if cur.left is None or cur.right is None:
                return max([do_tree(cur.left), do_tree(cur.right)]) + 1
            return min([do_tree(cur.left), do_tree(cur.right)]) + 1
        n = do_tree(root)
        return n
全部评论

相关推荐

2 2 评论
分享
牛客网
牛客企业服务