leetcode python  101.对称二叉树 104.二叉树的最大深度 112.路径总和

坚持一件事果然是很困难的,就比如写博客,几天不写就会很不想写。
三题都用的深搜求解
101
https://leetcode-cn.com/problems/symmetric-tree/description/
用深搜,注意是否会访问空指针,自己对于递归的理解还是差了点

class Solution(object):
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root==None:
            return True
        if(root.left!=None and root.right!=None):
            if self.dfs(root.left,root.right)==False:
                return False
            else:
                return True
        elif root.left==None and root.right==None:
            return True
        else:    
            return False


    def dfs(self,root11,root22):

        if(root11.val!=root22.val):
            return False
        if (root11.left!=None and root22.right==None) or (root11.left==None and root22.right!=None):
            return False
        elif root11.left!=None and root22.right!=None:
            if self.dfs(root11.left,root22.right)==False:
                return False
        if  (root22.left!=None and root11.right==None) or (root22.left==None and root11.right!=None):   
            return False
        elif root22.left!=None and root11.right!=None:
            if self.dfs(root22.left,root11.right)==False:
                return False
        return True

104.
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
深搜,递归

class Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        depth=0
        if root != None:
            depth+=1
        else:
            return 0
        depthl=depthr=depth
        if root.left!=None:
            depthl=self.dfs(root.left,depth+1)
        if root.right!=None:
            depthr=self.dfs(root.right,depth+1)
        if depthl>=depthr:
            return depthl
        else:
            return depthr

    def dfs(self,root,depth):
        depthl=depthr=depth
        if root.left!=None:
            depthl=self.dfs(root.left,depth+1)
        if root.right!=None:
            depthr=self.dfs(root.right,depth+1)
        if depthl>=depthr:
            return depthl
        else:
            return depthr

112.
https://leetcode-cn.com/problems/path-sum/description/

class Solution(object):
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        if root==None:
            return False
        x = root.val
        if root.left==None and root.right==None:
            if x==sum:
                return True
            else:
                return False
        if root.left!=None:
            if self.dfs(root.left,x,sum)==True:
                return True
        if root.right!=None:
            if self.dfs(root.right,x,sum)==True:
                return True
        return False    

    def dfs(self,root,x,sum):
        x+=root.val
        if root.right == None and root.left == None and x==sum:
            return True           
        if root.right!=None:
            if self.dfs(root.right,x,sum)==True:
                return True
        if root.left!=None:
            if self.dfs(root.left,x,sum)==True:
                return True
        return False

坚持真的是一件很不容易的事

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值