UVA548 - Tree
这个题目卡到我的地方
一是我对递归不熟,还没有建立二叉树和递归的关联。
二是,我把一个变量定义的太小了,(minn 的值定义得太小)。所以交了3遍的WA!!!
代码如下:
方法一:(把二叉树放到数组中)
#include <iostream>
#include <cstring>
using namespace std;
typedef struct
{
int data, leftnode, rightnode;
} Node;
int inorder[10050], postorder[10050], minn, lastnode, x;
Node node[10050];
int Build(int inleft, int inright, int postleft, int postrigth, int &fa)
{
int k, len;
if(inleft > inright || postleft > postrigth)return 0;
int p = fa = ++x;
node[x].data = postorder[postrigth];
for(k = inleft; k <= inright && inorder[k] != postorder[postrigth]; k++);
len = k - inleft ;
Build(inleft, k - 1, postleft, postleft + len - 1, node[p].leftnode);
Build(k + 1, inright, postleft + len, postrigth - 1, node[p].rightnode);
return 0;
}
int dfs(int i, int num)
{
num += node[i].data;
if(!node[i].leftnode && !node[i].rightnode)
{
if(num < minn)
{
minn = num;
lastnode = node[i].data;
}
else if(num == minn)
{
if(lastnode > node[i].data)
lastnode = node[i].data;
}
return 0;
}
if(node[i].leftnode) dfs(node[i].leftnode,num);
if(node[i].rightnode)dfs(node[i].rightnode,num);
return 0;
}
int main ()
{
int d, inlength, postlength;
while(1)
{
memset(node,0,sizeof(node));
inlength = postlength = 0;
while(1)
{
if((cin>>d) == 0)return 0;
inorder[inlength++] = d;
if(cin.get() == '\n')break;
}
while(1)
{
cin>>d;
postorder[postlength++] = d;
if(cin.get() == '\n')break;
}
//if(inlength != postlength)cout<<"error!!!"<<endl;
Build(0, inlength-1, 0, postlength-1, node[x = 0].rightnode);
lastnode = minn = 99999;
dfs(1, 0);
cout<<lastnode<<endl;
}
return 0;
}
方法二:(建立真正的二叉树)
#include <iostream>
#include <cstdlib>
using namespace std;
int inorder[10010], postorder[10010], minn, lastnode;
typedef struct Temp{
int data;
Temp *leftnode, *rightnode;
}Node;
int Buildtree(int inleft, int inright, int postleft, int postright, Node* &head)
{
int k, length;
if(inleft > inright || postleft > postright){head = NULL;return 0;}
head = (Node*)malloc(sizeof(Node));
head->data = postorder[postright];
//cout<<"build"<<" "<<head->data<<endl;
for(k = inleft; k <= inright && inorder[k] != postorder[postright];k++);
length = k - inleft;
Buildtree(inleft, k-1, postleft, postleft+length-1,head->leftnode);
Buildtree(k+1, inright, postleft+length, postright-1, head->rightnode);
return 0;
}
int dfs(Node* head, int sum)
{
sum+=head->data;//cout<<head->data<<" -> "<<sum<<endl;
if(!head->leftnode && !head->rightnode)
{
if(sum<minn){minn = sum;lastnode = head->data;}
else if(minn == sum && lastnode > head->data)
lastnode = head->data;
return 0;
}
if(head->leftnode)dfs(head->leftnode,sum);
if(head->rightnode)dfs(head->rightnode,sum);
return 0;
}
int main ()
{
int d, inlength, postlength;
while(1){
inlength = postlength = 0;
while(1){
if((cin>>d) == 0)return 0;
inorder[inlength++] = d;
if(cin.get() == '\n')break;
}
while(1){
cin>>d;
postorder[postlength++] = d;
if(cin.get() == '\n')break;
}
Node *head; //= (Node *)malloc(sizeof(Node));
Buildtree(0, inlength-1, 0, postlength-1, head);//cout<<"*****"<<endl;
minn = lastnode = 999999;
dfs(head, 0);
cout<<lastnode<<endl;
}
return 0;
}
浙公网安备 33010602011771号