当前位置:网站首页>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
边栏推荐
- How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
- 题解 Ananagrams(UVa156)紫书P113map的应用
- Fix the simulator that cannot be selected by flutter once
- Pie (poj3122) super detailed and easy to understand binary introduction
- Analysis of variance
- openGauss内核分析之查询重写
- 输入分隔符
- Real number operation
- Leetcode daily question - Sword finger offer II 091 Paint the house
- 3. integrate listener
猜你喜欢

MongoDB——副本集与分片

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
How to recover after Oracle delete accidentally deletes table data

How do I download videos? Look at the super simple method!
oracle delete误删除表数据后如何恢复

MySQL system error occurred 1067

RT thread thread synchronization and thread communication

Apisik helps Middle East social software realize localized deployment

方 差 分 析

Automatic operation and maintenance platform based on Apache APIs
随机推荐
Anr analysis - question 1
How to open an account in great wisdom? Is it safe
Pie (poj3122) super detailed and easy to understand binary introduction
穩定性總結
我也差点“跑路”
Bitbucket failed to pull the warehouse Using SSH
ThreadLocal principle
iterator中的next()为什么要强转?
Ehcache配置资料,方便自己查
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
Real number operation
Jenkins pipeline's handling of job parameters
How do I download videos? Look at the super simple method!
Leetcode 36. Effective Sudoku (yes, once)
RT thread thread synchronization and thread communication
各种类型长
input separator
Is it safe for CICC fortune to open an account? Let's talk about CICC fortune
方 差 分 析
MongoDB——副本集与分片