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

系统 1751 0

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

python 最大深度最小深度 LeetCode 104,111_第1张图片
python 最大深度最小深度 LeetCode 104,111_第2张图片
解法:
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的人,我????黑人问号!!!记录以下~~
python 最大深度最小深度 LeetCode 104,111_第3张图片


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
              
                )
              
              
                )
              
            
          

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论