Rebuilding a binary tree

Source: Internet
Author: User

Title Description: 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0016.gif "alt=" j_0016.gif "/> To enter the results of the pre-order traversal and the middle sequence traversal of a binary tree, rebuild the two-fork tree.

Assuming that the input two-fork tree has a pre-sequence traversal and the result of a middle-order traversal does not contain duplicate numbers

For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6} to re-build the two-fork tree and return the root node of the two fork tree

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/82/8B/wKioL1dZFumQ-BNWAAAspgCA7Rg822.png "title=" 1.png " alt= "Wkiol1dzfumq-bnwaaaspgca7rg822.png"/>

The nodes of the binary tree are defined as follows:

struct Binarytreenode{t _data; binarytreenode<t>* _left; binarytreenode<t>* _right; Binarytreenode (const t& x)//constructor: _data (x), _left (null), _right (null) {}};

Analysis: 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0020.gif "alt=" J_0020.gif "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/82/8B/wKioL1dZF_LAkLkoAADSlvMBuVY204.png "title=" 2.png " alt= "Wkiol1dzf_laklkoaadslvmbuvy204.png"/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/82/8B/wKioL1dZGBGDv_vSAACFzDRSPbw465.png "title=" 3.png " alt= "Wkiol1dzgbgdv_vsaacfzdrspbw465.png"/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/82/8C/wKiom1dZFzSx_FETAAA6FUokplA282.png "title=" 4.png " alt= "Wkiom1dzfzsx_fetaaa6fuokpla282.png"/>

At this point, we find the left and right subtree of the pre-sequence traversal and the results of the middle sequence traversal, we can use this method to continue to reconstruct the left and the sub-tree, so we can use recursion to solve this problem 650) this.width=650; "Src=" http://img.baidu.com/ Hi/jx2/j_0028.gif "alt=" J_0028.gif "/>

binarytreenode* rebuild (const int* prev, const  Int* in, int length, int& index, int left, int right) {assert ( Prev && in);if  (index >= length) return null; Binarytreenode* newnode = new binarytreenode (Prev[index]);if  (left == right return newnode;//find the position of the pre-order node in the middle sequence sequence Int inidx = findindex (In, prev[index], left,  right); if (inidx < 0) return null; Newnode->_left = rebuild (prev, in, n, ++index, left, inidx - 1 ); Newnode->_right = rebuild (prev, in, n, ++index, inidx + 1,  right); return newnode;} 

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" J_0057.gif "/> Where findindex (const int* A, const int& x, int left, int right) to find the subscript of x in array a

int FindIndex (const int* A, const int& x, int left, int. right) {for (int i = left; I <= right; ++i) {if (x = = A[i]) re Turn I;} return-1;}

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0060.gif "alt=" j_0060.gif "/> Note: parameters in the Index is a reference that is used to record the subscript of the current root node in the sequence of the preceding sequence, which should begin with 0.


This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1787644

Rebuilding a binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.