leetcode分类刷题:二叉树(六、构造二叉树)

构造二叉树的题目递归模板较为统一直观

106. 从中序与后序遍历序列构造二叉树

重复逻辑:不断构造根节点、划分左右子树和构建左右子树,非常直观的写法

python 复制代码
'''
106. 从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗二叉树。
示例 1:
    输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
    输出:[3,9,20,null,null,15,7]
思路:递归重复逻辑:不断构造根节点、划分左右子树和构建左右子树,非常直观的写法
'''
class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        # 简单情况
        if len(inorder) == 0 and len(postorder) == 0:
            return None
        else:  # 重复逻辑:不断构造根节点、划分左右子树和构建左右子树
            rootVal = postorder[-1]  # postorder: 左右根; 从postorder寻找根节点
            root = TreeNode(rootVal)
            idx = inorder.index(rootVal)  # inorder:左根右; 从inorder寻找划分index
            # 将inorder划分为左右子树
            inorderLeft = inorder[:idx]
            inorderRight = inorder[idx + 1:]
            # 将postorder划分为左右子树
            postorderLeft = postorder[:len(inorderLeft)]
            postorderRight = postorder[len(inorderLeft): -1]
            # 构建左右子树
            root.left = self.buildTree(inorderLeft, postorderLeft)
            root.right = self.buildTree(inorderRight, postorderRight)
            return root

105. 从前序与中序遍历序列构造二叉树

重复逻辑:不断构造根节点、划分左右子树和构建左右子树,非常直观的写法

python 复制代码
'''
105. 从前序与中序遍历序列构造二叉树
给定两个整数数组preorder 和 inorder,其中preorder 是二叉树的先序遍历, inorder是同一棵树的中序遍历,请构造二叉树并返回其根节点。
示例 1:
    输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
    输出: [3,9,20,null,null,15,7]
思路:递归重复逻辑:不断构造根节点、划分左右子树和构建左右子树,非常直观的写法
'''
class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        # 简单情况
        if len(preorder) == 0 and len(preorder) == 0:
            return None
        else:  # 重复逻辑:不断构造根节点、划分左右子树和构建左右子树
            rootVal = preorder[0]  # preorder: 根左右; 从preorder寻找根节点
            root = TreeNode(rootVal)
            idx = inorder.index(rootVal)  # inorder:左根右; 从inorder寻找划分index
            # 将inorder划分为左右子树
            inorderLeft = inorder[:idx]
            inorderRight = inorder[idx + 1:]
            # 将preorder划分为左右子树
            preorderLeft = preorder[1: len(inorderLeft) + 1]
            preorderRight = preorder[len(inorderLeft) + 1:]
            # 构建左右子树
            root.left = self.buildTree(preorderLeft, inorderLeft)
            root.right = self.buildTree(preorderRight, inorderRight)
            return root

654. 最大二叉树

重复逻辑:创建一个根节点,其值为nums 中的最大值;递归地在最大值左边的子数组前缀上构建左子树;递归地在最大值 右边 的子数组后缀上构建右子树

python 复制代码
'''
654. 最大二叉树
给定两个整数数组preorder 和 inorder,其中preorder 是二叉树的先序遍历, inorder是同一棵树的中序遍历,请构造二叉树并返回其根节点。
给定一个不重复的整数数组nums。最大二叉树可以用下面的算法从nums 递归地构建:
创建一个根节点,其值为nums 中的最大值。
递归地在最大值左边的子数组前缀上构建左子树。
递归地在最大值 右边 的子数组后缀上构建右子树。
返回nums 构建的 最大二叉树 。
示例 1:
    输入:nums = [3,2,1,6,0,5]
    输出:[6,3,5,null,2,0,null,null,1]
'''
class Solution:
    def constructMaximumBinaryTree(self, nums: List[int]) -> Optional[TreeNode]:
        # 简单情况
        if len(nums) == 0:
            return None
        else:  # 重复逻辑
            maxVal = max(nums)
            idx = nums.index(maxVal)
            root = TreeNode(maxVal)
            root.left = self.constructMaximumBinaryTree(nums[:idx])
            root.right = self.constructMaximumBinaryTree(nums[idx+1:])
            return root

108. 将有序数组转换为二叉搜索树

重复逻辑:不断划分数组、构造根节点、划分左右子树和构建左右子树

python 复制代码
'''
108. 将有序数组转换为二叉搜索树
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
示例 1:
    输入:nums = [-10,-3,0,5,9]
    输出:[0,-3,9,-10,null,5]
    解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:
思路:重复逻辑:不断划分数组、构造根节点、划分左右子树和构建左右子树
'''
class Solution:
    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
        # 高度平衡,意味着要不断二分
        # 简单情况
        if len(nums) == 0:
            return None
        else:  # 重复逻辑:划分数组、创建根节点、构建左右子树
            idx = (0 + len(nums) - 1) // 2  # 获取数组0~len(nums)-1的中间索引
            root = TreeNode(nums[idx])
            leftNums, rightNums = nums[:idx], nums[idx + 1:]
            root.left = self.sortedArrayToBST(leftNums)
            root.right = self.sortedArrayToBST(rightNums)
            return root
相关推荐
大二转专业5 小时前
408算法题leetcode--第24天
考研·算法·leetcode
__AtYou__11 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解
转调11 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
huanxiangcoco13 小时前
152. 乘积最大子数组
python·leetcode
希望有朝一日能如愿以偿14 小时前
力扣题解(飞机座位分配概率)
算法·leetcode·职场和发展
Espresso Macchiato14 小时前
Leetcode 3306. Count of Substrings Containing Every Vowel and K Consonants II
leetcode·滑动窗口·leetcode medium·leetcode 3306·leetcode周赛417
数据分析螺丝钉16 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
￴ㅤ￴￴ㅤ9527超级帅16 小时前
LeetCode hot100---数组及矩阵专题(C++语言)
c++·leetcode·矩阵
鱼跃鹰飞16 小时前
Leecode热题100-295.数据流中的中位数
java·服务器·开发语言·前端·算法·leetcode·面试
源代码•宸19 小时前
Leetcode—76. 最小覆盖子串【困难】
c++·经验分享·算法·leetcode·滑动窗口