当前位置:网站首页>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
边栏推荐
- On the complexity of software development and the way to improve its efficiency
- 实型数运算
- Pie (poj3122) super detailed and easy to understand binary introduction
- 03.hello_rust
- 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
- How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
- 题解 The Blocks Problem(UVa101)紫书P110vector的应用
- Application of Andy s first dictionary (uva10815) Purple Book p112set
- 【学习笔记】因子分析
- Characters and integers
猜你喜欢

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

LeetCode每日一题——515. 在每个树行中找最大值

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

我也差点“跑路”
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

数据标准化处理

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication

Leetcode 36. Effective Sudoku (yes, once)

Ehcache配置资料,方便自己查

Flatten of cnn-lstm
随机推荐
视频号如何下载视频?来看超简单方法!
CNN-LSTM的flatten
The principle and source code analysis of Lucene index construction
Data standardization processing
Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
题解 Pie(POJ3122)超详细易懂的二分入门
How to understand the usability of cloud native databases?
LeetCode每日一题——324. 摆动排序 II
Leetcode daily question - Sword finger offer II 091 Paint the house
如何使用 DataAnt 监控 Apache APISIX
mysql-发生系统错误1067
Ehcache配置资料,方便自己查
Lecture 30 linear algebra Lecture 4 linear equations
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
Flask——总结
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
实型数运算
Leetcode daily question - 324 Swing sort II
输入和输出实型数据
Characters and integers