当前位置:网站首页>【LeetCode】105. Constructing binary trees from preorder and inorder traversal sequences
【LeetCode】105. Constructing binary trees from preorder and inorder traversal sequences
2022-06-10 01:33:00 【LawsonAbs】
0. summary
- Blog source : [email protected]
1. subject
Construct a binary tree according to preorder traversal and inorder traversal .
2. thought
Key code : left_num = bound - in_left # You need to find the number of nodes on the left subtree
Because the left subtree also has a number , Therefore, it is required to pass the update parameters , The number of nodes in the left subtree should be taken into account , The same is true for right subtrees .
3. Code
# 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:
root = self.dfs(preorder,inorder,0,len(preorder)-1,0,len(inorder)-1)
return root
def dfs(self,preorder,inorder,pre_left,pre_right,in_left,in_right):
if pre_left > pre_right:
return None
# if pre_left == pre_right: # Only one node , Go straight back to
# return TreeNode(preorder[pre_left])
# Build up trees , The root node of the current tree
root_val = preorder[pre_left]
cur_root = TreeNode(root_val)
new_pre_left = pre_left + 1
new_pre_right = pre_right
# Find the left and right sides , Then build a tree separately
bound = inorder.index(root_val)
new_in_right = bound - 1
# You need to find the number of nodes on the left subtree
left_num = bound - in_left
# What is passed in here is still a complete array
# Build a left subtree
left = self.dfs(preorder,inorder,pre_left+1,pre_left+left_num,in_left,bound-1)
# Build right subtree
right = self.dfs(preorder,inorder,pre_left+left_num+1,pre_right,bound+1,in_right)
cur_root.left = left
cur_root.right = right
return cur_root
边栏推荐
- Random talk on test coverage
- Win11右键怎么直接打开所有选项?
- 985毕业,35岁创业失败,36岁回炉40岁被裁,中年夫妻无业咋办?
- Locust: a powerful tool for microservice performance testing
- What are the reasons for frequent channel drops in easycvr cascaded video convergence platform?
- 电脑系统怎么修改图片格式
- iNFTnews | NFT在Web3经济里的未来
- CocosCreator旧活新整-合成大粽子
- JVM records a CPU surge
- 免费批量导入生成sitemap地图的软件
猜你喜欢

缓解修复Android Studio卡顿,Kotlin代码提示慢

免费批量导入生成sitemap地图的软件

Introduction to cross platform multimedia rendering engine OPR
[email protected] 项目实训"/>[email protected] 项目实训

What are the good techniques for domestic spot silver: simple usage of common indicators

Trichview and scalericview set default Chinese

Web3 is crucial to the data sovereignty of the meta universe

【Multisim仿真】差分比例放大电路
[email protected]和[email protected]装载5-Fu(5-氟尿"/>[email protected]和[email protected]装载5-Fu(5-氟尿

From these papers in 2022, we can see the trend of recommended system sequence modeling
随机推荐
软件工程期末复习
为什么芯片设计也需要「匠人精神」?
【LeetCode】338. 比特位计数
Why do programmers run away after two years?
Various utilization forms of map tile data and browsing display of tile data
How can I right-click win11 to open all options directly?
Software engineering final review
Introduction to cross platform multimedia rendering engine OPR
The writing speed is increased by tens of times. The application of tdengine in tostar intelligent factory solution
Where is safe and reliable for Hangzhou futures to open an account?
Win11右键怎么直接打开所有选项?
【Multisim仿真】差分比例放大电路
iNFTnews | NFT在Web3经济里的未来
MySQL script for Linux Installation
Application of discrete time integration in Simulink simulation
Batch downloading of pictures + mosaic of pictures: multiple pictures constitute the Dragon Boat Festival Ankang
Graduated in 985, failed to start a business at the age of 35, returned at the age of 36, and was laid off at the age of 40. What should middle-aged couples do if they are unemployed?
Sunyuchen et al. Acquired poloniex, the two wheel drive wave field ecology of the public chain exchange
Force deduction solution summary 497 random points in non overlapping rectangles
【无标题】416. 分割等和子集