当前位置:网站首页>力扣树的进一步应用
力扣树的进一步应用
2022-06-28 20:47:00 【Steven迪文】
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]) -> TreeNode:
if not nums: return
n = nums.index(max(nums)) # 找出当前最大的节点
left = self.constructMaximumBinaryTree(nums[:n]) # 利用分治, 递归构造左右子树
right = self.constructMaximumBinaryTree(nums[n + 1:])
root = TreeNode(nums[n], left, right) #后序遍历位置,从下至上逐层构建树
return root
105. 从前序与中序遍历序列构造二叉树
解题思路:
- 前序遍历顺序的第一个节点为根节点。
- 利用根节点在中序遍历顺序中的索引位置,划分前序与中序列表,逐层递归。
# 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]) -> TreeNode:
if not preorder or not inorder: return None
root = TreeNode(preorder[0])
# 因为没有重复元素,所以可以根据值来查找根节点在中序遍历中的位置
midIndex = inorder.index(preorder[0])
root.left = self.buildTree(preorder[1 :midIndex + 1], inorder[:midIndex])
root.right = self.buildTree(preorder[midIndex + 1:], inorder[midIndex + 1:])
return root
106. 从中序与后序遍历序列构造二叉树
解题思路:
- 后序遍历顺序的最后一个节点为根节点。
- 利用根节点在中序遍历顺序中的索引位置,划分后序与中序列表,逐层递归。
# 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, inorder: List[int], postorder: List[int]) -> TreeNode:
if not inorder or not postorder:
return None
root = TreeNode(postorder[-1])
midIndex = inorder.index(postorder[-1])
root.left = self.buildTree(inorder[:midIndex], postorder[:midIndex])
root.right = self.buildTree(inorder[midIndex + 1:], postorder[midIndex:-1])
return root
889. 根据前序和后序遍历构造二叉树
解题思路:
- 前序遍历顺序的第一个节点为根节点,后序遍历顺序的最后一个节点为根节点。
- 采用前序遍历的方式,在前序遍历顺序中,第二个元素为左子树根节点的索引。
- 利用左子树根节点在后序遍历中的位置,拆分前序和后序数组,递归构建子树。
# 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 constructFromPrePost(self, preorder: List[int], postorder: List[int]) -> TreeNode:
if not preorder or not postorder:
return None
root = TreeNode(preorder[0])
# 因为不是寻找根节点,所以需要判断当前部分树的长度,如果pre的长度是1,直接返回root即可
if len(preorder) == 1:
return root
midIndex = postorder.index(preorder[1]) # 第二个元素为左子树根节点的索引。
root.left = self.constructFromPrePost(preorder[1:midIndex+2], postorder[:midIndex+1])
root.right = self.constructFromPrePost(preorder[midIndex+2:], postorder[midIndex+1:-1])
return root
边栏推荐
- 03.hello_ rust
- Understand the construction of the entire network model
- 阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
- 题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
- 输入和输出字符型数据
- 基于 Apache APISIX 的自动化运维平台
- 【Try to Hack】Cobalt Strike(一)
- T检验(检验两个总体的均值差异是否显著)
- Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
- Leetcode daily question - 710 Random numbers in the blacklist
猜你喜欢

ThreadLocal principle

Data standardization processing

MySQL system error occurred 1067

Visualization of neural network structure in different frames

C # connect to the database to complete the operation of adding, deleting, modifying and querying

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

视频号如何下载视频?来看超简单方法!

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

如何使用 DataAnt 监控 Apache APISIX

ThreadLocal原理
随机推荐
APISIX 助力中东社交软件,实现本地化部署
Bitbucket 使用 SSH 拉取仓库失败的问题
How to recover after Oracle delete accidentally deletes table data
Résumé de la stabilité
题解 Pie(POJ3122)超详细易懂的二分入门
ThreadLocal principle
UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation
No module named ‘PyEMD‘ ;使用plt.figure()TypeError: ‘module‘ object is not callable
Understanding of incomplete types
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
我也差点“跑路”
输入分隔符
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
RT-Thread线程同步与线程通信
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
How to understand the fast iteration of cloud native database?
【学习笔记】主成分分析法介绍
Apisik helps Middle East social software realize localized deployment
Understand the construction of the entire network model
Data standardization processing