1099. Build A Binary Search Tree (30)建立二叉搜索树
来源:互联网 发布:程序员和老师结婚 编辑:程序博客网 时间:2024/06/03 20:59
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format “left_index right_index”, provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.
Sample Input:91 62 3-1 -1-1 45 -1-1 -17 -1-1 8-1 -173 45 11 58 82 25 67 38 42Sample Output:
58 25 82 11 38 67 45 73 42
#include<iostream>#include<algorithm>#include<vector>using namespace std;struct tree{int num,left,right;};struct tree T[10000];int num[10000];int index1=0;int max_lev=1;vector<int > leval[1000];void fun1(int root)//按顺序树里填值{ if (T[root].left==-1&&T[root].right==-1) { T[root].num=num[index1++]; return ; } if(T[root].left!=-1) fun1(T[root].left); T[root].num=num[index1++]; if(T[root].right!=-1) fun1(T[root].right);}void fun2(int root,int lev)//查找每一层的数{ if (max_lev<lev) max_lev=lev; leval[lev].push_back(T[root].num); if(T[root].left!=-1) fun2(T[root].left,lev+1); if(T[root].right!=-1) fun2(T[root].right,lev+1);}int main(){ int n; cin>>n; for(int i=0;i<n;i++) cin>>T[i].left>>T[i].right; for (int i=0;i<n;i++) cin>>num[i]; sort(num,num+n);//二叉搜索树的中序遍历是非下降序列。 fun1(0); fun2(0,1); for (int i=1;i<=max_lev;i++) { for (int j=0;j<leval[i].size();j++) { cout<<leval[i][j]; if (j!=leval[i].size()&&i!=max_lev) cout<<" "; } } return 0;}
- 1099. Build A Binary Search Tree (30)建立二叉搜索树
- 1099. Build A Binary Search Tree (30)[bst二叉搜索树]
- 1099. Build A Binary Search Tree (30) 给定二叉搜索树插值
- pat 甲 1099. Build A Binary Search Tree (二叉搜索树)
- PAT 1099. Build A Binary Search Tree (30)建立二叉排序树
- 1099. Build A Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- 1099. Build A Binary Search Tree (30)
- Nodejs ssh2 exec执行shell超时终止执行
- Video http dash mp4 box slice
- 6. vue.js-饿了吗全套-项目文件介绍
- spring和mybatis整合
- 深度学习笔记——理论与推导之Structured Learning【Learning with Hidden Information】(九)
- 1099. Build A Binary Search Tree (30)建立二叉搜索树
- 快速排序算法
- Symbol
- 系列化
- stm32 Bootloader设计(YModem协议)
- linuxc网络通信
- Windows10安装Tensorflow1.3
- Learning Nginx
- presto集群安装以及集成kerberos