LEETCODE-DAY20


title: LEETCODE-DAY20
date: 2024-03-11 18:37:07
tags:

今日内容:654.最大二叉树 、617.合并二叉树 、700.二叉搜索树中的搜索、98.验证二叉搜索树

T1

class Solution:
    def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
        root_val=max(nums)
        root=TreeNode(root_val)
        idx=nums.index(root_val)

        nums_left=nums[:idx]
        nums_right=nums[idx+1:]
        root.left=self.constructMaximumBinaryTree(nums_left)
        root.right=self.constructMaximumBinaryTree(nums_right)

        return root

ValueError: max() arg is an empty sequence
^^^^^^^^^
root_val=max(nums)

class Solution:
    def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
        if not nums:
            return None
        root_val=max(nums)
        root=TreeNode(root_val)
        idx=nums.index(root_val)

        nums_left=nums[:idx]
        nums_right=nums[idx+1:]
        root.left=self.constructMaximumBinaryTree(nums_left)
        root.right=self.constructMaximumBinaryTree(nums_right)

        return root

AC

T2

class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root1 and not root2:
            return None
        elif root1 and not root2:
            root_val=root1.val
        elif not root1 and root2:
            root_val=root2.val
        else:
            root_val=root1.val+root2.val
        root=TreeNode(root_val)

        
        root.left=self.mergeTrees(root1.left,root2.left)
        root.right=self.mergeTrees(root1.right,root2.right)

        return root

AttributeError: ‘NoneType’ object has no attribute ‘left’
^^^^^^^^^^
root.left=self.mergeTrees(root1.left,root2.left)

问题在于没有保证进入递归时root1与root2均非空

class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root1:
            return root2
        if not root2:
            return root1
        root_val=root1.val+root2.val
        root=TreeNode(root_val)

        
        root.left=self.mergeTrees(root1.left,root2.left)
        root.right=self.mergeTrees(root1.right,root2.right)

        return root


T3

class Solution:
    def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        cur=root
        while cur:
            if val==cur.val:
                return cur
            elif val < cur.val:
                cur=cur.left
            else:
                cur=cur.right
        return None

EZ

T4

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        if  root.left and root.val <root.left .val:
            return False
        if root.right and root.val > root.right.val:
            return False
        self.isValidBST(root.left)
        self.isValidBST(root.right)

输入
root =
[2,1,3]
输出
null
预期结果
true

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        if root.left and root.val <root.left .val:
            return False
        if root.right and root.val > root.right.val:
            return False
        return self.isValidBST(root.left) and self.isValidBST(root.right)

输入
root =
[2,2,2]
输出
true
预期结果
false

def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        if root.left and root.val <=root.left .val:
            return False
        if root.right and root.val >= root.right.val:
            return False
        return self.isValidBST(root.left) and self.isValidBST(root.right)

输入
root =
[5,4,6,null,null,3,7]
输出
true
预期结果
false

此处bug在于右孩子的左孩子应该还有下界要求,即大于其祖父节点的值
同理左孩子的右孩子也应该有上界要求

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        if root.left and root.val <=root.left .val:
            return False
        elif root.right and root.val >= root.right.val:
            return False
        elif root.left and root.left.right and root.left.right.val >=root.val:
            return False
        elif root.right and root.right.left and root.right.left.val <=root.val:
            return False
        else:
            return self.isValidBST(root.left) and self.isValidBST(root.right)

输入
root =
[120,70,140,50,100,130,160,20,55,75,110,119,135,150,200]
输出
true
预期结果
false

这样写太复杂,应该找一个min记录父节点之前的最小值

迭代写法

class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        q=deque()
        if root:
            q.append(root)
        while q:



递归写法(直接看中序遍历的结果是否递增即可)

class Solution:
    def inorder(self,root):
        if not root:
            return []
        left=self.inorder(root.left)
        right=self.inorder(root.right)
        return left+[root.val]+right
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        res=self.inorder(root)
        return res==sorted(res)

输入
root =
[2,2,2]
输出
true
预期结果
false

class Solution:
    def inorder(self,root):
        if not root:
            return []
        left=self.inorder(root.left)
        right=self.inorder(root.right)
        return left+[root.val]+right
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        res=self.inorder(root)
        for i in range(len(res)-1):
            if res[i]>=res[i+1]:
                return False
        return True

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值