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

二叉树的最小深度

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
全部评论

相关推荐

2025-12-15 14:25
云南大学 Java
lei22:入职可能会看学信网,最好别伪装,这个简历找实习肯定是够的,肯定会有收 28 届实习生的公司的,多投就行
点赞 评论 收藏
分享
评论
3
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务