HDU 1710

http://acm.hdu.edu.cn/showproblem.php?pid=1710

题意:给二叉树的先序遍历和中序遍历,确定后序遍历

解法:复习专业课找的一题,根据先序遍历和中序遍历建树,再对树做后序遍历

#include <iostream>
#include <cstdio>

using namespace std;

struct Tree {
    Tree *lc, *rc;
    int data;
};

Tree *Create(int *preorder, int *inorder, int n) {
    for(int i = 0; i < n; i++) {
        if(preorder[0] == inorder[i]) {
            Tree *node = (Tree*)malloc(sizeof(Tree));
            node -> data = preorder[0];
            node -> lc = Create(preorder+1, inorder, i);
            node -> rc = Create(preorder+i+1, inorder+i+1, n-i-1);
            return node;
        }
    }
    return NULL;
}

int rt;

void PostOrder(Tree *root) {
    if(root != NULL) {
        PostOrder(root -> lc);
        PostOrder(root -> rc);
        if(root -> data == rt)
            printf("%d\n", root->data);
        else
            printf("%d ", root->data);
    }
}

Tree *root;
int preorder[1005], inorder[1005]; 

int main() {
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; i++)
            scanf("%d", &preorder[i]);
        for(int i = 0; i < n; i++)
            scanf("%d", &inorder[i]);
        root = NULL;
        rt = preorder[0];
        root = Create(preorder, inorder, n);
        PostOrder(root);
    }
    return 0;
}

时间: 2025-01-11 13:48:15

HDU 1710的相关文章

HDU 1710 二叉树遍历,输入前、中序求后序

1.HDU  1710  Binary Tree Traversals 2.链接:http://acm.hust.edu.cn/vjudge/problem/33792 3.总结:记录下根结点,再拆分左右子树,一直搜下去.感觉像dfs. 题意:二叉树,输入前.中序求后序. (1)建立出一颗二叉树,更直观.但那些指针用法并不是很懂. #include<iostream> #include<cstdio> #include<cstdlib> using namespace

HDU 1710 Binary Tree Traversals(二叉树)

题目地址:HDU 1710 已知二叉树先序和中序求后序. #include <stdio.h> #include <string.h> int a[1001], cnt; typedef struct node { int date ; node *lchild , *rchild ; }*tree; int getk(int ch,int ino[],int is,int n) { for(int i = is ; i <= is + n -1 ; i++) if(ino[

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

hdu 1710 二叉树的遍历

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1710 大意:给出一个二叉树的前序和中序,求其后序遍历 ps:1.在写链表时,需要写明typedef struct node{};即声明一个指向自己的数据类型,而不是直接写struct node{} 2.采用分治的思想,把一颗二叉树分解成n棵二叉树,每棵都有其对应的根节点,利用这点进行遍历 3.malloc在#include<stdlib.h>头文件中 4.这是自己第一次写数据结构树型题目,所以代码是借

【二叉树】hdu 1710 Binary Tree Traversals

acm.hdu.edu.cn/showproblem.php?pid=1710 [题意] 给定一棵二叉树的前序遍历和中序遍历,输出后序遍历 [思路] 根据前序遍历和中序遍历递归建树,再后续遍历输出 malloc申请空间在堆,函数返回,内存不释放,需要free手动释放 [Accepted] #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<alg

HDU 1710 二叉树的遍历 Binary Tree Traversals

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4330    Accepted Submission(s): 1970 Problem Description A binary tree is a finite set of vertices that is either empty or

Binary Tree Traversals HDU - 1710 ?

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically

HDU 1710 二叉树三种遍历

Binary Tree Traversals Problem Description A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the verti

HDU 1710 Binary Tree Traversals

题意:给出一颗二叉树的前序遍历和中序遍历,输出其后续遍历 首先知道中序遍历是左子树根右子树递归遍历的,所以只要找到根节点,就能够拆分出左右子树 前序遍历是按照根左子树右子树递归遍历的,那么可以找出这颗树的根节点, 然后拆分出左右子树,对左右子树进行相同的操作,也就是将建树的这个函数递归调用下去 build函数还是理解了好久啊话说= =仍然是学习的代码 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring>