uva 548

#include
#include
#include
#include
using namespace std;
const int MAXN = 10005;
int inOrder[MAXN], postOrder[MAXN], nIndex;

class Node{
public:
    int data;
    Node *left;
    Node *right;
};

int nodeIndex;
Node node[MAXN];
vectorresult;
vectorpResult;
bool flag;
int ans;

inline Node* NewNode(){
    node[nodeIndex].left = NULL;
    node[nodeIndex].right = NULL;
    return &node[nodeIndex++];
}
 
inline void input(){
    nIndex=1;
    while(getchar()!='\n') 
        scanf("%d", &inOrder[nIndex++]);
    // 输入第二行,后序遍历
    for(int i=0; idata=post[len-1];
	h->left=InPostCreateTree(mid,post,i);
	h->right=InPostCreateTree(mid+i+1,post+i,len-i-1);
	return h;
}

void dfs(Node *root, int n){
    if(!root->left && !root->right){
        result.push_back(n+root->data);
        pResult.push_back(root);
        return ;
    }
    if(root->left) dfs(root->left, n+root->data);
    if(root->right) dfs(root->right, n+root->data);
}

int main(){
    freopen("input.txt","r",stdin);
    while(~scanf("%d", &inOrder[0])){
        input();
        nodeIndex = 0;
        Node *root = InPostCreateTree(inOrder, postOrder, nIndex);
        result.clear();
        pResult.clear();
        dfs(root, 0);
        int minPos = 0;
        for(int i=1; idata);
    }  
    return 0;
}

你可能感兴趣的:(ACM)