代码随想录算法训练营第20天|654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

654.最大二叉树 

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树_哔哩哔哩_bilibili

解题思路:

前序遍历

设置basic condition 如果nums为空的话 直接返回

进入递归 找到最大的值并设置成当前root 以其index左右分成两个part 先向左递归再向右递归 返回上来的的root值做左右子树的值

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def constructMaximumBinaryTree(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        if not nums:
            return
        max_val=max(nums)
        max_index=nums.index(max_val)
        root=TreeNode(max_val)
        left_part=nums[:max_index]
        right_part=nums[max_index+1:]
        root.left=self.constructMaximumBinaryTree(left_part)
        root.right=self.constructMaximumBinaryTree(right_part)
        return root

617.合并二叉树 

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树_哔哩哔哩_bilibili

解题思路:

先判断第一个树是不是为空 为空的话返回另一个树 然后判断第二个树是不是空 如果是空的话 直接返回最开始的树。反之 如果两个树此时的节点都不为空的话 进入前序遍历递归 将此时第二个节点的值加到第一个节点  接着进行左递归和右递归。 

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def mergeTrees(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: TreeNode
        """
        if not root1:
            return root2
        if not root2:
            return root1
        root1.val+=root2.val
        root1.left = self.mergeTrees(root1.left,root2.left)
        root1.right = self.mergeTrees(root1.right,root2.right)
        return root1
        

700.二叉搜索树中的搜索 

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解: 代码随想录

视频讲解:不愧是搜索树,这次搜索有方向了!| LeetCode:700.二叉搜索树中的搜索_哔哩哔哩_bilibili

解题思路:

basic condition 如果node为空直接返回空 如果node的值等于target直接返回当前node

进入左递归如果node当前值大于target  反之进去右递归。 

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def searchBST(self, root, val):
        """
        :type root: TreeNode
        :type val: int
        :rtype: TreeNode
        """
        if not root:
            return None
        if root.val==val:
            return root
        elif root.val>val:
            return self.searchBST(root.left,val)
        elif root.val<val:
            return self.searchBST(root.right,val)

98.验证二叉搜索树 

题目链接:LeetCode - The World's Leading Online Programming Learning Platform

题目链接/文章讲解:代码随想录

视频讲解:你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树_哔哩哔哩_bilibili

解题思路:

搜索树的中序遍历是increasing order

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def isValidBST(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        res=[]
        def traversal(root):
            if not root:
                return
            traversal(root.left)
            res.append(root.val)
            traversal(root.right)
        def is_increasing(ls):
            for i in range(len(ls)-1):
                if ls[i]>=ls[i+1]:
                    return False
            return True
        traversal(root)
        return is_increasing(res)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值