uva 548 Tree

点击打开链接uva 548

思路: 二叉树
分析:
1 题目给定一棵二叉树的中序序列和后序序列求这个二叉树里面,根节点到叶子节点的最短路径的叶子节点的值,如果有多条路径输出最小的叶子节点
2 题目说最多有10000个节点,但是由于题目所说的二叉树并不是完全二叉树,所以这里建立二叉树并不能够利用静态的思想,应该要利用动态的增加
3 建立了二叉树之后,只要按照前序遍历的思路即可求出ans

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int INF = 1<<30;
const int MAXN = 1000010;

struct Node{
    int x;
    Node *left;
    Node *right;
    Node(int x){
        this->x = x;
        this->left = NULL;
        this->right = NULL;
    }
};
Node *root;

char str[MAXN];
int nodeNum , ans , maxNum , stepNum;
int midOrder[MAXN] , postOrder[MAXN];

void getOrder(int *arr){
    int len = strlen(str);
    nodeNum = 0;
    for(int i = 0 ; i < len ; i++){
        int j = i;
        int num = 0;
        while(str[j] != ' ' && j < len){
            num = num*10 + str[j]-'0';
            j++;
        }
        arr[nodeNum++] = num;
        i = j;
    }
}

Node* createTree(int *mid , int *post , int len){
    if(len == 0)
        return NULL;
    int k = 0;
    while(mid[k] != post[len-1])
        k++;
    Node *root = new Node(post[len-1]);
    // 左子树
    root->left = createTree(mid , post , k);
    // 右子树
    root->right = createTree(mid+k+1 , post+k , len-k-1);
    return root;
}

void solve(int sum , int step , Node *node){
    if(node != NULL){
        if(node->left == NULL && node->right == NULL){
            if(maxNum > sum+node->x){
                maxNum = sum+node->x;
                ans = node->x;
            }
            else if(maxNum == sum+node->x)
                ans = min(ans , node->x);
        }
        solve(sum+node->x , step+1 , node->left);
        solve(sum+node->x , step+1 , node->right);
    }
}    

int main(){
    while(gets(str)){
        getOrder(midOrder);
        gets(str);
        getOrder(postOrder);

        root = createTree(midOrder , postOrder , nodeNum);
        maxNum = ans = INF;
        solve(0 , 0 , root);
        printf("%d\n" , ans);
    }
    return 0;
}
时间: 2024-09-21 11:49:54

uva 548 Tree的相关文章

UVa 548 Tree:中序遍历&amp;amp;后序遍历&amp;amp;DFS

548 - Tree Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 You are to determine the value of the leaf node in a given binary tree that is the termina

UVa 548:Tree 二叉树的重建——中序遍历与后续遍历进行建树

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 题目类型: 数据结构, 二叉树 题目大意: 给两个组数字,都是在同一棵二叉树上的,第一组是按中序遍历(inorder)顺序输出的,第二组是按后序遍历(postorder)输出的, 根据这两组数据构建出原来的二叉树,然后计算从根结点到每个叶子结

UVa 152 Tree&#039;s a Crowd (暴力)

152 - Tree's a Crowd Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=88 Dr William Larch, noted plant psychologist and inventor of the phrase ``Think like

UVa 112 Tree Summing (scanf()去空格&amp;amp;递归&amp;amp;二叉树遍历)

112 - Tree Summing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48 Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one o

uva 536 - Tree Recovery

点击打开链接uva 536 思路: 二叉树 分析: 1 题目给定前序序列和中序序列,要求二叉树的后序序列 2 建好二叉树之和直接遍历输出即可,裸题 代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 30; char preOrder[MAXN]; char midOrder[MA

uva 112 Tree Summing

点击打开链接 题目意思:给定一个字符串,把字符串里面的数字建成一颗树,数字有可能是负号,所以这一题其实有很多可以学的,比如怎样输入会有多行,还有怎么建立一颗树,等等. 解题思路:我们用一个字符数组来存储读入的字符串,用一个栈来存储括号,判断如果栈为空并且读到换行那么就退出.我们可以先把根节点建好,建根节点调用creatnode()函数,注意数字可能是1234    -1234这些情况,所以判断.然后我们在从根节点后面的位置开始变量字符数组,如果是('(')则判断下一额是数字还是右括号,如果是数字

UVa 10247 Complete Tree Labeling:组合数学&amp;amp;高精度

10247 - Complete Tree Labeling Time limit: 15.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1188 A complete k-ary tree is a k-ary tree in which all leaves have same d

UVa 112:Tree Summing 二叉树构造, 二叉树路径和

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=48&mosmsg=Submission+received+with+ID+10280379 题目类型: 数据结构, 二叉树 加强版样例输入: 22 (5(4(11(7()())(2()()))()) (8(13()())(4()(1()())))

UVa 11503:Virtual Friends

题目链接: UVa : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2498 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=3172 类型:   并查集, 哈希 原题: These days, you can do all sorts of things on