PAT 1099. Build A Binary Search Tree (30)
2015-07-27 19:05
134 查看
这道题是二叉搜索树(BST)。
1.根据节点建树
2.把数组数排序
3.中序遍历二叉树,讲数据写入树中
4.层次遍历树
1.根据节点建树
2.把数组数排序
3.中序遍历二叉树,讲数据写入树中
4.层次遍历树
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct p { int left; int right; int value; bool flag;//是否已经有数字插入 }Node[100001]; int point[101]; int len = 0; void insert(int i) { if(Node[i].left != -1) insert(Node[i].left); Node[i].value = point[len++]; if(Node[i].right != -1) insert(Node[i].right); } int main() { int n; while(scanf("%d",&n) != EOF) { for(int i = 0; i < n; i++) { scanf("%d%d",&Node[i].left,&Node[i].right); Node[i].flag = false;//没有用到 } for(int i = 0; i < n; i++) scanf("%d",&point[i]); sort(point,point+n);//排序 insert(0);//遍历 queue<int> q; q.push(0); while(!q.empty()) { int x = q.front(); q.pop(); if(x != 0) printf(" "); printf("%d",Node[x].value); if(Node[x].left != -1) q.push(Node[x].left); if(Node[x].right != -1) q.push(Node[x].right); } printf("\n"); } return 0; }
相关文章推荐
- flash builder 配置中如果有中文会报错
- iOS - UI-归档解归档
- POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)
- leetcode 060 —— Permutation Sequence
- hdu 5306 Gorgeous Sequence
- Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别
- 【读书笔记】UIWindow-WindowLevel
- 【读书笔记】UIWindow-WindowLevel
- codeforce Gym 100500H ICPC Quest (简单dp)
- Unique Binary Search Trees
- UITableViewRowAction的使用
- html元素li的value属性
- iOS-UI-01 UIWindow UIView
- hdu5033 Building 单调队列
- UIView及其子类
- iOS Error: build settings specify a provisioning profile with the UUID
- MVC interview question
- B - Build The Electric System --- (fisrt qualifying)
- UEFI中的Protocol浅谈
- Daikon Forge GUI Library(dfgui)之Event Binding