当前位置:网站首页>The further application of Li Kou tree
The further application of Li Kou tree
2022-06-28 20:48:00 【Steven Devin】
654. The largest binary tree

Their thinking :
- Find the largest node in each layer .
- The left and right subtrees are constructed layer by layer from bottom to top .
# 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)) # Find the current largest node
left = self.constructMaximumBinaryTree(nums[:n]) # Use divide and conquer , Recursively construct left and right subtrees
right = self.constructMaximumBinaryTree(nums[n + 1:])
root = TreeNode(nums[n], left, right) # Postorder traversal position , Build the tree layer by layer from the bottom up
return root
105. Construction of binary trees from traversal sequences of preorder and middle order
Their thinking :
- The first node in the preorder traversal sequence is the root node .
- Use the index position of the root node in the middle order traversal order , Divide the pre order and middle order lists , Layer by layer recursion .
# 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])
# Because there are no repeating elements , So you can find the position of the root node in the middle order traversal according to the value
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. Construct binary tree from middle order and post order traversal sequence
Their thinking :
- The last node in the post order traversal sequence is the root node .
- Use the index position of the root node in the middle order traversal order , Divide the post order and middle order lists , Layer by layer recursion .
# 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. Construct binary tree according to preorder and postorder traversal
Their thinking :
- The first node in the preorder traversal sequence is the root node , The last node in the post order traversal sequence is the root node .
- Using preorder traversal , In the preorder traversal sequence , The second element is the index of the root node of the left subtree .
- Use the position of the root node of the left subtree in the post order traversal , Split the pre order and post order arrays , Recursively build subtrees .
# 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])
# Because it's not looking for the root node , So you need to determine the length of the current part of the tree , If pre Is the length of the 1, Go straight back to root that will do
if len(preorder) == 1:
return root
midIndex = postorder.index(preorder[1]) # The second element is the index of the root node of the left subtree .
root.left = self.constructFromPrePost(preorder[1:midIndex+2], postorder[:midIndex+1])
root.right = self.constructFromPrePost(preorder[midIndex+2:], postorder[midIndex+1:-1])
return root
边栏推荐
猜你喜欢

Lecture 30 linear algebra Lecture 4 linear equations

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

ThreadLocal principle

Lucene构建索引的原理及源代码分析

Bitbucket failed to pull the warehouse Using SSH

学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证

Analysis of variance

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

我也差点“跑路”

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
随机推荐
关键字long
oracle delete误删除表数据后如何恢复
How to understand the fast iteration of cloud native database?
LeetCode每日一题——710. 黑名单中的随机数
How to understand the usability of cloud native databases?
Flatten of cnn-lstm
题解 Ananagrams(UVa156)紫书P113map的应用
图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
2. 整合 Filter
【学习笔记】主成分分析法介绍
穩定性總結
How do I download videos? Look at the super simple method!
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
with torch. no_ Grad(): reason for using
圆球等的相关计算
Bitbucket failed to pull the warehouse Using SSH
Bitbucket 使用 SSH 拉取仓库失败的问题
openGauss内核分析之查询重写
[try to hack] cobalt strike (I)
Comparisonchain file name sort