hot100练习-11

python 复制代码
# 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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        def recur(root, left, right):
            if left > right: return
            node = TreeNode(preorder[root])
            i = dic[preorder[root]]
            node.left = recur(root+1, left, i-1)
            node.right = recur(i-left+root+1, i+1, right)
            return node

        dic = {}
        for i in range(len(inorder)):
            dic[inorder[i]] = i
        return recur(0, 0, len(inorder) - 1)

这里用前序确定根节点位置,用中序确定left和right

  • ​​前序遍历(preorder)​​:根节点 → 左子树 → 右子树(第一个元素是根节点)

  • ​​中序遍历(inorder)​​:左子树 → 根节点 → 右子树(根节点左侧是左子树,右侧是右子树)

通过前序遍历确定根节点,然后在中序遍历中找到该根节点的位置,从而划分左右子树的范围,递归构建整棵树

python 复制代码
# 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]:
        if not root1: return root2
        if not root2: return root1
        return TreeNode(root1.val+root2.val, self.mergeTrees(root1.left, root2.left), self.mergeTrees(root1.right, root2.right))
python 复制代码
# 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 flatten(self, root: Optional[TreeNode]) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        if not root: return None
        left_tail = self.flatten(root.left)
        right_tail = self.flatten(root.right)
        if left_tail:
            left_tail.right = root.right
            root.right = root.left
            root.left = None
        return right_tail or left_tail or root

分别找到左右子树的末尾节点,然后连接左右节点并都放到右节点上

python 复制代码
class Solution:
    def leastInterval(self, tasks: List[str], n: int) -> int:
        cnt = Counter(tasks)
        nbucket = cnt.most_common(1)[0][1]
        last_bucket_size = list(cnt.values()).count(nbucket)
        res = (nbucket - 1) * (n + 1) + last_bucket_size
        return max(res, len(tasks))
        

设计桶大小为n+1,相同的任务不能放在同一个桶里面,然后对于重复的任务,我们只能将每个都放入不同的桶中,因此桶的个数就是重复次数最多的任务的个数。一个桶不管是否放满,其占用的时间均为 n+1,这是因为后面桶里的任务需要等待冷却时间。最后一个桶是个特例,由于其后没有其他任务需等待,所以占用的时间为桶中的任务个数。所以

总排队时间 = (桶个数 - 1) * (n + 1) + 最后一桶的任务数

last_bucket_size = list(ct.values()).count(nbucket)

计算有多少个任务和最高频任务一样频繁,就是最后一个桶肯定放的都是最高频任务,那么就是最后一个桶的任务数了。

python 复制代码
class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        m, n = len(board), len(board[0])
        def dfs(i, j, k):
            if not 0 <= i < m or not 0 <= j < n or board[i][j] != word[k]: return False
            if k == len(word) - 1: return True
            board[i][j] = ''
            res = dfs(i-1, j, k+1) or  dfs(i+1, j, k+1) or dfs(i, j+1, k+1) or dfs(i, j-1, k+1)
            board[i][j] = word[k]
            return res
        
        for i, row in enumerate(board):
            for j, e in enumerate(row):
                if dfs(i, j, 0): return True
        return False

判断,置空,递归,恢复,返回

相关推荐
地平线开发者3 小时前
征程 6 | 工具链如何支持 Matmul/Conv 双 int16 输入量化?
人工智能·算法·自动驾驶
甄心爱学习3 小时前
数值计算-线性方程组的迭代解法
算法
stolentime3 小时前
SCP2025T2:P14254 分割(divide) 题解
算法·图论·组合计数·洛谷scp2025
Q741_1474 小时前
C++ 面试基础考点 模拟题 力扣 38. 外观数列 题解 每日一题
c++·算法·leetcode·面试·模拟
W_chuanqi4 小时前
RDEx:一种效果驱动的混合单目标优化器,自适应选择与融合多种算子与策略
人工智能·算法·机器学习·性能优化
L_09074 小时前
【Algorithm】二分查找算法
c++·算法·leetcode
靠近彗星4 小时前
3.3栈与队列的应用
数据结构·算法
Han.miracle8 小时前
数据结构——二叉树的从前序与中序遍历序列构造二叉树
java·数据结构·学习·算法·leetcode
mit6.82410 小时前
前后缀分解
算法