Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to reconstruct python binary tree

Shulou Source: shulou.com Published: 2022-06-01 14:15:05 09月16日 Update

Today, I would like to talk to you about how to rebuild the python binary tree. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

Title:

Enter the results of preorder traversal and mid-order traversal of a binary tree, please reconstruct the binary tree. Assume that the results of both the preorder traversal and the intermediate traversal of the input do not contain duplicate numbers. For example, if you enter the preorder ergodic sequence {1 ~ (th), 2 ~ (th), 4 ~ (th), 7 ~ (th), and 3 ~ (th), then the binary tree is rebuilt and returned.

Train of thought:

When I see this topic, I understand that it will take some time. When I take the data structure class, I understand how to determine a binary tree according to the preorder and intermediate order, and I also know how to rebuild it, but it is still a bit difficult to implement it in code. After all, I haven't written it for a long time. I know that to achieve that idea, it must be necessary to borrow recursion or merge. But it is not very clear, after learning from others how to achieve, after reading, decided to do it yourself, at first written in java, after writing, there are null pointer exception and array illegal access errors, and other people's code for a long time, and finally found the error, this question trapped me for a day. For null pointer exceptions, it is best to initialize the object or declare the array by yourself. Do not wait until later to remember that the illegal access to the array is due to the fact that the array declared by myself is too large and not very rigorous. As a result, I couldn't find where I was wrong, and I didn't feel out of bounds. After strictly changing the size of the array to the required requirements of the topic, I submitted it successfully. I may have been used to writing C before. If we do not pay attention to this aspect, we should be more rigorous in the future. It is much more convenient and smooth to implement with js code than Java. Although there are also errors, after solving the java pit, it is much faster to solve the Js pit.

For this kind of problem, the main thing is to figure out how to use recursive implementation and pay attention to details.

Java implementation code:

Public class Solution {

Public TreeNode reConstructBinaryTree (int [] pre,int [] in) {

Int len=in.length

If (len==0)

Return null

TreeNode tree=new TreeNode (pre [0])

Int itempence, temptation, copyright 1, jockey 0

For (iSuppli)

Tags: Code array or title subtree content error input rigor sequence idea pointer time result recursion clarity difficulty success right size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno NVidia Xiaomi Docker Shulou Technology