-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path32BinaryTreeTrverse1078.cpp
71 lines (66 loc) · 1.54 KB
/
32BinaryTreeTrverse1078.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<cstring>
using namespace std;
class Node{
public:
Node *lchild;
Node *rchild;
char c;
Node(){lchild=rchild=NULL;}
};
void clear(Node *root)
{
if(root!=NULL)
{
clear(root->lchild);
clear(root->rchild);
root->lchild=NULL;
root->rchild=NULL;
delete root;
root=NULL;
}
}
char str1[30],str2[30];//save preorder and inorder result
void PostOrder(Node *root)//后序遍历
{
if(root->lchild!=NULL)
PostOrder(root->lchild);
if(root->rchild!=NULL)
PostOrder(root->rchild);
cout<<root->c;
}
//还原二叉树
Node *Build(int s1,int e1,int s2,int e2)//s1:前序遍历的开始,结束。s2:中序遍历的开始位置和结束位置。
{
Node *ret=new Node();
ret->c=str1[s1];//根节点
int rootIndex;
for(int i=s2;i<=e2;i++)
{
if(str1[s1]==str2[i])
{
rootIndex=i;//查找中序遍历中的根节点的位置
break;
}
}
if(rootIndex!=s2)//如果中序遍历的左子树不为空
ret->lchild=Build(s1+1,s1+(rootIndex-s2),s2,rootIndex-1);//it's important to identify the boundary
if(rootIndex!=e2)//如果右子树不为空
ret->rchild=Build(s1+(rootIndex-s2)+1,e1,rootIndex+1,e2);
return ret;
}
int main()
{
while(cin>>str1)
{
cin>>str2;
int len1=strlen(str1);
int len2=strlen(str2);
Node *root=new Node();
root=Build(0,len1-1,0,len2-1);
PostOrder(root);
cout<<endl;
clear(root);
}
return 0;
}