中序遍历:CBDEAFG
后序遍历:CEDBGFA
(1)先根据后序遍历结果找出树的根结点A。
(2)然后将中序遍历分成三部分 CBDE A FG。
(3)同样将后序结果分成 CEDB GF A三部分(根据左右子树结点个数相等分)。
CBDE 和CEDB分别是根结点的右子树的中序和后序遍历结果,FG和GF分别为根结点的左子树的中序和后序遍历结果,然后依次递归。。。
参考:http://blog.csdn.net/wuchuanpingstone/article/details/6860453
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node
{
char data;
struct node *rchild,*lchild;
}BiTNode,*BiTree;
void InOrder(BiTree T)//中序遍历
{
if(T==NULL)
{
return;
}
InOrder(T->lchild);
printf("%c ",T->data);
InOrder(T->rchild);
}
void PostOrder(BiTree T) //后序遍历
{
if(T==NULL)
{
return;
}
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c ",T->data);
}
void Creat_in_post(char *in,char *post,int len,BiTree &T) //in为中序遍历结果 post为后序遍历结果 len为当前序列的长度
{
if(len<=0)
{
T=NULL;
return;
}
int k;
for (char *temp=in; temp<in+len; temp++)
{
if(*temp==post[len-1])
{
k=temp-in; //k为当前树的根结点
T=(BiTree)malloc(sizeof(BiTNode));
T->data=*temp;
break;
}
}
Creat_in_post(in, post,k, T->lchild); //递归建立左子树
Creat_in_post(in+k+1, post+k, len-k-1, T->rchild); //递归建立右子树
}
int main()
{
BiTree T;
char in[20],post[20];
printf("请输入中序结果:");
scanf("%s",in);
getchar();
printf("请输入后序结果:");
scanf("%s",post);
int len=strlen(in);
Creat_in_post(in, post,len,T);
printf("由中序和后序求解树:\n");
printf("中序遍历:");
InOrder(T);
printf("\n");
printf("后序遍历:");
PostOrder(T);
printf("\n");
//ABCDEFG
//CBDEAFG
//CEDBGFA
return 0;
}
UVA 548
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.
Input
The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.
Output
For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.
Sample Input
3 2 1 4 5 7 6 3 1 2 5 6 7 4 7 8 11 3 5 16 12 18 8 3 11 7 16 18 12 5 255 255
Sample Output
1 3 255
一颗二叉树用中序和后序遍历给出,让你求出从根到叶子节点的总和最小的叶子节点的值。
先根据中序和后序创建出二叉树,然后再用dfs搜一次就行了。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int Min;
typedef struct BinaryTree
{
int data;
BinaryTree *lchild,*rchild;
BinaryTree()
{
data=0;
lchild=rchild=NULL;
}
}BTree;
BTree *ans;
int inOrder[10005];
int postOrder[10005];
int cnt;
void CreatTree(BTree *&root,int *in,int *post,int len) //创建二叉树
{
if(len<=0)
{
root=NULL;
return;
}
int k=0;
for (int i=0; i<len; i++)
{
if(in[i]==post[len-1])
{
root=new BTree();
root->data=in[i];
k=i;
break;
}
}
CreatTree(root->lchild, in, post, k);
CreatTree(root->rchild, in+k+1, post+k, len-k-1);
}
void dfs(BTree *root,int data) //搜索和最小的叶子结点
{
if(!root->lchild && !root->rchild)
{
if(Min>data+root->data)
{
Min=data+root->data;
ans=root;
return;
}
}
if(root->lchild)
{
dfs(root->lchild, data+root->data);
}
if(root->rchild)
{
dfs(root->rchild, data+root->data);
}
}
void Delete(BTree *&root) //删除二叉树
{
if (root==NULL)
{
return;
}
Delete(root->lchild);
Delete(root->rchild);
delete root;
root=NULL;
}
int main()
{
BTree *Root=NULL;
while (scanf("%d",&inOrder[0])!=EOF)
{
cnt=1;
Min=10000000;
Delete(Root);
while(getchar()!='\n')
{
scanf("%d", &inOrder[cnt++]);
}
for(int i=0; i<cnt; ++i)
{
scanf("%d", &postOrder[i]);
}
CreatTree(Root, inOrder, postOrder, cnt);
dfs(Root, 0);
printf("%d\n",ans->data);
}
return 0;
}