python 最大深度最小深度 LeetCode 104,111

32 篇文章 1 订阅
29 篇文章 0 订阅

python 最大深度最小深度 LeetCode 104,111



解法:
1、BFS:寻找最大深度的时候,很容易想到就是,可以直接进行层次遍历,当无法在进行遍历下去的时候就是最深的深度;当寻找最小深度的时候,对每一个节点检查它是否是叶子节点,也就是检查它是否有左子树和右子树。

2、DFS:每次进行遍历的时候,要判断是否是叶子节点,更新max深度的值和min深度的值。


BFS版本

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:return 0
        if not root.right and not root.left: return 1
        cur = [root]
        maxnum = 0
        while cur:
            nextstack,tmp = [],[]
            for node in cur:
                tmp.append(node.val)
                if node.left:
                    nextstack.append(node.left)
                if node.right:
                    nextstack.append(node.right)
            cur = nextstack
            maxnum +=1
        return maxnum
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:return 0
        if not root.right and not root.left: return 1
        cur = [root]
        minnmb = 0
        while cur:
            nextstack,tmp = [],[]
            for node in cur:
                tmp.append(node.val)
                if node.left:
                    nextstack.append(node.left)
                if node.right:
                    nextstack.append(node.right)
            for node in cur:
                if not node.left and not node.right:
                    minnmb +=1
                    return minnmb
            if node == cur[-1]:
                minnmb +=1
            cur = nextstack
        

上面的代码居然提交,打败了99.89的人,我????黑人问号!!!记录以下~~


DFS版本

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        return 1+max(self.maxDepth(root.right),self.maxDepth(root.left))
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        if not root.left:
            return 1 + self.minDepth(root.right)
        if not root.right:
            return 1 + self.minDepth(root.left)
        
        return 1+ min(self.minDepth(root.right),self.minDepth(root.left))
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值