当前位置:网站首页>力扣树的进一步应用
力扣树的进一步应用
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
边栏推荐
- [graduation season · advanced technology Er] hard work can only pass, hard work can be excellent!
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- 学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
- Flask——总结
- Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
- 阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
- 稳定性总结
- Is the inter-bank certificate of deposit reliable and safe
- 1. integrate servlets
- How to understand the usability of cloud native databases?
猜你喜欢

【学习笔记】主成分分析法介绍

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用

Ref attribute, props configuration, mixin mixing, plug-in, scoped style
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)
![[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences](/img/7a/16b481753d7d57f50dc8787eec8a1a.png)
[go language questions] go from 0 to entry 5: comprehensive review of map, conditional sentences and circular sentences

不同框架的绘制神经网络结构可视化

ThreadLocal原理
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

Flatten of cnn-lstm

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
随机推荐
The blocks problem (uva101) Purple Book p110vector application
Bitbucket 使用 SSH 拉取仓库失败的问题
APISIX 助力中东社交软件,实现本地化部署
Anr analysis - question 1
实型数运算
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
API 网关 Apache APISIX 助力雪球双活架构演进
【Try to Hack】Cobalt Strike(一)
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
不同框架的绘制神经网络结构可视化
How to use dataant to monitor Apache apisex
ThreadLocal原理
[learning notes] factor analysis
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
国产数据库名录一览
软件watchdog和ANR触发memory dump讲解
1. integrate servlets
RT thread thread synchronization and thread communication
市值1200亿美金,老牌财税巨头Intuit是如何做到的?