LeetCode第108 110 111 112题

  1. 将有序数组转换为二叉搜索树(LeetCode第108题)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        
        if not nums:
            return None
        
        numsLen = len(nums)
        midIndex = numsLen // 2
        root = TreeNode(nums[midIndex])
        root.left = self.sortedArrayToBST(nums[:midIndex]) #比root小的放在左子树
        root.right = self.sortedArrayToBST(nums[midIndex+1:]) #比root大的放在右子树
        
        return root
  1. 平衡二叉树(LeetCode第110题)
    方法一:
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        
        self.flag = True
        
        def maxDepth(root):
            
            if not root:
                return 0
            
            lDepth = maxDepth(root.left)
            rDepth = maxDepth(root.right)
            
            if abs(lDepth - rDepth) > 1:
                self.flag = False
                
            return max(lDepth,rDepth) + 1
        
        maxDepth(root)
        if self.flag == False:
            return False
        else:
            return True

方法二:利用之前判定二叉树最大深度的函数

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
 
class Solution:
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root == None:
            return True
        elif abs(self.height(root.left)-self.height(root.right))>1: #就是root.left和root.right往下的深度
            return False
        else: 
            return self.isBalanced(root.left) and self.isBalanced(root.right)
    
    def height(self,root):
        if root == None:
            return 0
        else:
            return max(self.height(root.left),self.height(root.right))+1
  1. 二叉树的最小深度(LeetCode第111题)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        
        if root is None:
            return 0
        if root.left == None and root.right == None:
            return 1
        elif root.left == None:
            return 1 + self.minDepth(root.right)
        elif root.right == None:
            return 1 + self.minDepth(root.left)
        else:
            return 1 + min(self.minDepth(root.left),self.minDepth(root.right))
  1. 路径总和(LeetCode第112题)
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        
        if not root:
            return False
        
        sum -= root.val
        if sum == 0:
            if root.left is None and root.right is None:
                return True
        return self.hasPathSum(root.left,sum) or self.hasPathSum(root.right,sum)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值