算法训练 Day19

654.最大二叉树

解法:递归(与前序后序构建一个二叉树类似方法)

# 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
        
        node = max(nums)

        root = TreeNode(node)

        indx = nums.index(node)
        leftrange = nums[:indx]
        rightrange = nums[indx+1:]
        
        root.left = self.constructMaximumBinaryTree(leftrange)
        root.right = self.constructMaximumBinaryTree(rightrange)
        return root

617.合并二叉树

解法1:递归

# 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]:
        # method 1(前序)
        if not root1:
            return root2
        if not root2:
            return root1
        
        node = root1.val + root2.val
        root = TreeNode(node)

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

解法2:迭代

# 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]:
        # method 2
        if not root1:
            return root2
        if not root2:
            return root1
        
        queue = [root1,root2]
        while queue:
            node1 = queue.pop(0)
            node2 = queue.pop(0)

            if node1.left and node2.left:
                queue.append(node1.left)
                queue.append(node2.left)
            if node1.right and node2.right:
                queue.append(node1.right)
                queue.append(node2.right)
            
            node1.val += node2.val
            if not node1.left and node2.left:
                node1.left = node2.left
            if not node1.right and node2.right:
                node1.right = node2.right
        return root1

700.二叉搜索树中的搜索

解法1:递归

# 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]:
        # method 1
        if not root or root.val == val:
            return root
        
        if root.val > val:
            res = self.searchBST(root.left,val)
        if root.val < val:
            res = self.searchBST(root.right,val)
        return res

解法2:迭代

# 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]:
        # method 2
        while root:
            if root.val > val:
                root = root.left
            elif root.val < val:
                root = root.right
            else:
                return root
        return None

98.验证二叉搜索树

解法:递归

# 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:
        # method 1(中序遍历)
        res = []
        def inorder(root):
            nonlocal res

            if not root:
                return None
            inorder(root.left)
            res.append(root.val)
            inorder(root.right)
        
        inorder(root)
        return res == sorted(set(res))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值