1 /**********************************************************
 2 题目:   Binary Tree Traversals(hdu 1710)
 3 链接:   http://acm.hdu.edu.cn/showproblem.php?pid=1710
 4 题意:   给出二叉树的先序和中序遍历结果,求后序遍历结果
 5 算法:   二叉树
 6 思路:   先以先序和中序建树,先序的第一个点是根节点,中序
 7          和先序第一个点相同的位置前所有点都是左子树,所以
 8          就可以递归来建树,最后给树后序遍历就可以了。
 9 ***********************************************************/
10 #include<cstdio>
11 #include<cstring>
12 #include<algorithm>
13 #include<iostream>
14 #include<cstdlib>
15 using namespace std;
16
17 const int mx=1004;
18 typedef struct Tree
19 {
20     Tree *left,*right;
21     int num;
22 }Tree;
23 Tree *tree;
24
25 Tree *getree(int *a,int *b,int n)
26 {
27     Tree *s;
28
29     for (int i=0;i<n;i++)
30     {
31         if (b[i]==a[0])
32         {
33             s=(Tree *)malloc(sizeof(Tree));
34             s->num=a[0];
35             s->left=getree(a+1,b,i);
36             s->right=getree(a+i+1,b+i+1,n-i-1);
37             return s;
38         }
39     }
40     return NULL;
41 }
42
43 void Find(Tree *p)
44 {
45     if (p==NULL) return ;
46     Find(p->left);
47     Find(p->right);
48     if (p==tree) printf("%d\n",p->num);
49     else printf("%d ",p->num);
50     delete(p);
51 }
52
53 int main()
54 {
55     int a[mx],b[mx];
56     int n;
57     while (~scanf("%d",&n))
58     {
59         for (int i=0;i<n;i++) scanf("%d",&a[i]);
60         for (int i=0;i<n;i++) scanf("%d",&b[i]);
61         tree=getree(a,b,n);
62         Find(tree);
63     }
64 }

 1 /********************************************************
 2 这题也可边建树边输出
 3 *********************************************************/
 4 #include<iostream>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<cstdlib>
 8 using namespace std;
 9
10 const int mx=1004;
11
12 void dfs(int *a,int *b,int n,int flag)
13 {
14     for (int i=0;i<n;i++)
15     {
16         if (a[0]==b[i])
17         {
18             dfs(a+1,b,i,0);
19             dfs(a+i+1,b+i+1,n-i-1,0);
20             if (flag) printf("%d\n",a[0]);
21             else printf("%d ",a[0]);
22         }
23     }
24 }
25
26 int main()
27 {
28       int a[mx],b[mx];
29       int n;
30       while (~scanf("%d",&n))
31       {
32           for (int i=0;i<n;i++) scanf("%d",&a[i]);
33           for (int i=0;i<n;i++) scanf("%d",&b[i]);
34           dfs(a,b,n,1);
35       }
36 }

转载于:https://www.cnblogs.com/pblr/p/5732101.html

hdu 1710 Binary Tree Traversals (二叉树)相关推荐

  1. (二叉树存储+递归遍历)Binary Tree Traversals

    题目: A binary tree is a finite set of vertices that is either empty or consists of a root r and two d ...

  2. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树

    反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...

  3. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /*** Definition for a bi ...

  4. [LeetCode] Binary Tree Paths - 二叉树基础系列题目

    目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...

  5. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

  6. leetcode 637. Average of Levels in Binary Tree | 637. 二叉树的层平均值(Java)

    题目 https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/ 题解 1.参考"二叉树按层打印"写的解法 ...

  7. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  8. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

    题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...

最新文章

  1. 【Java】牛客网 删除链表中重复的结点
  2. *:教育产品 规范销售
  3. 1、RabbitMQ入门
  4. python_Django之模板模型
  5. as3分页——页数多了后自动居中类似谷歌分页
  6. 给asterisk1.8.7添加menuselct选项
  7. java resource file_Java 获取Resource目录下的文件解决办法
  8. xunit-ICollectionFixture
  9. TensorFlow之Vscode调试
  10. php7 生成.so,centos8 php7 生成 openssl.so 文件执行make时报错:【已解决】
  11. LNMP详解(十三)——Nginx子页面详解
  12. shell unexpected operator
  13. ABAQUS2021界面改成中文
  14. J-LINK 软件下载地址
  15. jfinal+poi导出excel
  16. 【PAT甲级 单源最短路径】1087 All Roads Lead to Rome (30 分)
  17. 使用JOL工具直接查看出java对象大小
  18. 百度编辑器ueditor,编辑内容过多时,工具栏不可见,不方便编辑或上传问题
  19. 中国联通cdma 1x和中国移动gprs数据业务比较
  20. Kibana快速上手

热门文章

  1. 为什么有些WIFI不能用万能钥匙搜索到?怎么才能破解邻居家的WIFI密码?
  2. 自驾游,遇陌生夫妻搭车坐了一千多公里,执意要给钱,我能收吗?
  3. 散酒到底多少钱才是纯酿的
  4. 一个人想生存发展具备3大关键
  5. 为什么创业你只为少数人服务就够了?
  6. 新媒体运营的“钱途”在哪里?
  7. 如何让你的员工有闭环思维?
  8. SQL Server中的动态SQL
  9. POJ3714 Raid 分治/K-D Tree
  10. Spring Boot2.0之性能优化