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

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

你可能感兴趣的:(Leetcode)