In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about how to calculate the preorder traversal and middle order traversal of the python binary tree. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Idea: when you see the algorithm of binary tree, the first thing that comes to mind is traversal.
Now that the preorder traversal and intermediate order traversal are given, I calculate the preorder traversal and intermediate order traversal of the left subtree, and the preorder traversal and intermediate order traversal of the right subtree each time.
Main program
Int len = pre.length
If (len = = 0) {
Return null
}
Else {
TreeNode tnode = new TreeNode (pre [0])
Int loc = getindex (pre [0], in)
Int [] preL = getsubarr (1, loc, pre); / / preorder traversal of the left subtree
Int [] preR = getsubarr (0, loc-1, in); / / the mid-order traversal of the left subtree is equal in length
Int [] inL = getsubarr (loc+1,len-1, pre); / / preorder traversal of the right subtree
Int [] inR = getsubarr (loc+1,len-1, in); / / the length of the middle order traversal of the right subtree is equal.
Tnode.left = getarr (preL, preR)
Tnode.right = getarr (inL, inR)
Return tnode
}
# A function that defines a given sequence and target value and finds the index of the target value in the sequence
Public int getindex (int target, int [] array) {
Int len = array.length
For (int iTuno; I < len; iTunes +) {
If (target = = array [I]
Return i
}
}
Define the start and end subscripts of a given array and give the corresponding fields for the array
Public int [] getsubarr (int start, int end, int [] arr) {
Int [] sub = new int [end-start+1]
For (int iTunes start; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
I hope you will read it carefully.
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.