代码随想录训练营Day20|● 654.最大二叉树 ● 617.合并二叉树 ● 700.二叉搜索树中的搜索 ● 98.验证二叉搜索树

目录

学习目标

学习内容

654.最大二叉树 

 617.合并二叉树

 700.二叉搜索树中的搜索

 98.验证二叉搜索树


学习目标

  • 654.最大二叉树
  • 617.合并二叉树
  • 700.二叉搜索树中的搜索
  • 98.验证二叉搜索树

学习内容

654.最大二叉树 

654. 最大二叉树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/maximum-binary-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
        if not nums:return None
        n = len(nums)
        target = 0
        index = 0
        for i in range(n):
            if nums[i]>=target:
                target = nums[i]
                index = i
        root = TreeNode(val = nums[index])
        root.left = self.constructMaximumBinaryTree(nums[0:index])
        root.right = self.constructMaximumBinaryTree(nums[index+1:n])
        return root

 617.合并二叉树

617. 合并二叉树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/merge-two-binary-trees/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        def creat_tree(root1,root2):
            if not root1:return root2
            elif not root2:return root1
            else:
                root1.val += root2.val
                root1.left = creat_tree(root1.left, root2.left)
                root1.right = creat_tree(root1.right,root2.right)
                return root1
        return creat_tree(root1,root2)

 700.二叉搜索树中的搜索

700. 二叉搜索树中的搜索 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/search-in-a-binary-search-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        if not root:return None
        if root.val == val:return root
        elif root.val<val:
            root = self.searchBST(root.right,val)
        else:
            root = self.searchBST(root.left,val)
        return root

 98.验证二叉搜索树

98. 验证二叉搜索树 - 力扣(LeetCode)icon-default.png?t=N3I4https://leetcode.cn/problems/validate-binary-search-tree/

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        def search_tree(root):
            if not root:return []
            return search_tree(root.left)+[root.val]+search_tree(root.right)
        res = search_tree(root)
        n = len(res)
        for i in range(1,n):
            if res[i]<=res[i-1]:
                return False
        return True

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值