Binary Tree Traversals (树的重建)
来源:互联网 发布:网线接入网络方式 编辑:程序博客网 时间:2024/04/29 09:22
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 traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.
In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.
In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.
Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
91 2 4 7 3 5 8 9 64 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1
代码1
#include <iostream>using namespace std;int in[1005];int pre[1005];int pos=0;int n;void dfs(int a, int b, int len){ if (len == 1) { cout << pre[a]; if(pos==n-1) cout<<'\n'; else cout<<' '; pos++; return; } else if (len <= 0) return; int i; for (i=0; pre[a] != in[b + i]; i++); dfs(a+1,b, i);//左子树 dfs(a + 1 + i, b + i + 1, len - i - 1);//右子树 { cout << pre[a] ; if(pos==n-1) cout<<'\n'; else cout<<' '; pos++; }}int main(){ while (cin >> n) { pos=0; for (int i = 1; i <= n; i++) cin >> pre[i]; for (int i = 1; i <= n; i++) cin >> in[i]; dfs(1, 1, n); //cout << endl; }}
代码2
#include<iostream>#include<string.h>#include<algorithm>#include<vector>#include<stdio.h>using namespace std;int n,pos=0;vector<int>pre,in,post;void rec(int l,int r){ if(l>r) { return ; } int root=pre[pos++]; int m=distance(in.begin(),find(in.begin(),in.end(),root)); rec(l,m-1); rec(m+1,r); post.push_back(root);}void solve(){ rec(0,pre.size()-1); for(int i=0; i<n; i++) { printf("%d ",post[i]); } printf("\n"); return ;}int main(){ while(cin>>n) { int a; pos=0; pre.clear(); in.clear(); post.clear(); for(int i=0; i<n; i++) { cin>>a; pre.push_back(a); } for(int i=0; i<n; i++) { cin>>a; in.push_back(a); } solve(); } return 0;}
0 0
- Binary Tree Traversals (树的重建)
- HDU 1710-Binary Tree Traversals(重建二叉树)
- HDU1710 Binary Tree Traversals(二叉树重建模板609)
- 重建二叉树 Binary Tree Traversals HDU
- HDU Binary Tree Traversals(二叉树重建+dfs)
- HDU1710 Binary Tree Traversals【树的遍历】
- Binary Tree Traversals(二叉树)
- 1020. Tree Traversals (25) (重建二叉树)
- PAT 1020 Tree Traversals(树重建+BFS)
- 数据结构之二叉树的遍历 HDU1710 Binary Tree Traversals
- hdu 1710 Binary Tree Traversals 二叉树的遍历
- HDU 1710(Binary Tree Traversals)二叉树的遍历
- HDU 1710 - Binary Tree Traversals(树的遍历)
- HDOJ 1710Binary Tree Traversals 二叉树的遍历
- HDU1710 Binary Tree Traversals(DFS,二叉树的遍历)
- HDU1710——Binary Tree Traversals(二叉树)
- HDU 1710 Binary Tree Traversals(二叉树)
- HDOJ 题目1710 Binary Tree Traversals(二叉搜索树)
- reactnative 解决listview无法滚动的问题
- Opencv mat 多元线性回归
- Merge into在单表中的应用实例
- HTTP协议中的短轮询、长轮询、长连接和短连接
- int最大值2147483647
- Binary Tree Traversals (树的重建)
- 硬件创业如何避免一些坑
- 如果使用追加标志O_APPEND打开一个文件以便读、写,能否仍能用lseek在任一位置开始读? 能否用lseek更新文件中任一部分的数据?
- 关于实际使用https需要注意的一个小点
- 如何列出某个目录下的所有目录和文件
- 前端必学-----AJax--笔记---02
- 重叠IO模型2
- 深入springMVC
- mybatis mybatis-generator 代码自动生成工具