Rebuilding Binary Tree _ C ++ and binary tree _ c

Source: Internet
Author: User

Rebuilding Binary Tree _ C ++ and binary tree _ c

I. Question Background

Returns the forward and middle traversal of a binary tree.

For more information about binary tree traversal, see

Http://blog.csdn.net/fansongy/article/details/6798278/

Ii. Algorithm Analysis

For example, the binary tree below

Its first traversal is dbacepidermal

Its central order traversal is: ABCDEFG

Its post-order traversal is: ACBFGED

First, point to the first character in the first order, that is, the root node D of the tree.

Then, locate D in the middle-order traversal and divide the traversal into ABC and EFG. Because the Middle-order traversal is based on the structure of the left-right structure, we can see that the left-hand subtree of D is its right subtree, and the right-hand subtree is its right subtree on the left.

Go to its left subtree ABC. At this time, the pointer is + 1, pointing to B

Locate B in the left subtree ABC and divide it into two parts: A and C. A is the left subtree and C is the right subtree.

Pointer correspondingly + 2

This keeps recursion until all nodes are found.

The overall idea is to find the root node of the sub-tree from the first-order traversal, and then recursively traverse the left and right sides in the middle-order traversal. At the same time, each node is added with a pointer + 1 for the first-order traversal, it can be proved that this method is correct

To determine whether a binary tree can be formed, you only need to determine whether the root node can be found. If not, the binary tree cannot be formed.

 1 #include <algorithm> 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 #include <cmath> 7 #define N 10000 8 using namespace std; 9 10 char mid[N],frt[N];11 int k,cr[N],cl[N];12 int bt(int l,int r)13 {14     if (l>r) return -1;15     if (l==r)16         {17             k++;18             return l;19         }20     int i;21     for (i=l;i<=r;i++)22         {23             if (frt[k]==mid[i])24                 {25                     break;26                 }27         }28     k++;29     cl[i]=bt(l,i-1);30     cr[i]=bt(i+1,r);31     return i;32 }33 void outp(int x)34 {35     if (x==-1) return;36     outp(cl[x]);37     outp(cr[x]);38     cout<<mid[x];39 }40 int main()41 {42     int len,i;43     freopen("bt.in","r",stdin);44     freopen("bt.out","w",stdout);45     gets(frt);46     gets(mid);47     k=0;48     len=strlen(mid);49     for (i=0;i<=len;i++) cl[i]=cr[i]=-1;50     outp(bt(0,len-1));51     cout<<endl;52     return 0;53 }

 

Iii. Question Source

Jiudu Oline Judge question 1385: rebuilding a binary tree (this requires determining whether it can be built)

Http://ac.jobdu.com/problem.php? Pid = 1, 1385

Nanyang Institute of Technology Online evaluation system question 756: rebuilding a binary tree (this is the input of the central order and the post order traversal, find the first order traversal)

Http://acm.nyist.net/JudgeOnline/problem.php? Pid = 1, 756

 

 

 

 

All rights reserved. For details, contact the author.

QQ: 740929894

Related Article

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.