In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.