Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to deduce the follow-up according to the preorder and intermediate order of Java binary tree

Shulou Source: shulou.com Published: 2022-06-01 16:27:27 11月14日 Update

This article introduces the relevant knowledge of "how to introduce the follow-up of Java binary tree according to the pre-order and middle order". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

According to pre-order and middle order = > post-order

# include#include#includeusing namespace std;struct BTreeNode {int _ value; BTreeNode*_left; BTreeNode*_right;}; / / solution-BTreeNode* RebuildCode (int * PreStart, int * PreEnd, int * InStart, int * InEnd) {BTreeNode* root = new BTreeNode (); / / New node root saves the first node in the preorder as root node root- > _ value = PreStart [0]; root- > _ left = NULL; root- > _ right = NULL If (InStart = = InEnd & & * InStart = = * InEnd) {return root;} / / find the location where the node traverses this node in the middle order int * rootIn = InStart; while (* PreStart! = * rootIn) {rootIn++;} / / the length of the left subtree int leftlen = rootIn-InStart / / rebuild the left subtree if (leftlen > 0) {root- > _ left = RebuildCode (PreStart+1, PreStart+leftlen, InStart,InStart+leftlen-1);} / rebuild the right subtree if (InStart+leftlen

< InEnd) { root->

_ right = RebuildCode (PreStart+leftlen+1, PreEnd,InStart+leftlen+1, InEnd);} return root;} BTreeNode* RebuildTree (int * PreOrder, int * InOrder, int len) {/ / first determine the boundary condition if (PreOrder = = NULL | | InOrder = = NULL | | len _ left! = NULL) {PostOrder (root- > _ left);} if (root- > _ right! = NULL) {PostOrder (root- > _ right) } if (root! = NULL) {cout 0) {root- > _ left = RebuildCode (PostStart, PostStart+leftlen-1, InStart,InStart+leftlen-1);} / / rebuild the right subtree if (InStart+leftlen

< InEnd) { root->

_ right = RebuildCode (PostStart+leftlen, PostEnd-1, InStart+leftlen+1, InEnd);} return root;} BTreeNode* RebuildTree (int * PostOrder, int * InOrder, int len) {/ / first judge the boundary condition if (PostOrder = = NULL | | InOrder = = NULL | | len _ right);}} int main () {int PostOrder [8] = {7pr.

Tags: Node subtree location content accordingly more condition knowledge node solution boundary length output utility achievement next dilemma reality sequence situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MySQL NVidia MariaDB Shulou Information