pat1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
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:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42Sample Output:
58 25 82 11 38 67 45 73 42
平衡二叉树。每次找到中间节点。
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node{
int l,r,v;
};
node tree[];
int line[];
int calnum(int num){
if(num==-){
return ;
}
return calnum(tree[num].l)+calnum(tree[num].r)+;
}
void BuildAVL(int mid,int *line){
if(mid==-){
return ;
}
int count=calnum(tree[mid].l);//统计左子树 //cout<<count<<endl; tree[mid].v=line[count];
BuildAVL(tree[mid].l,line);
BuildAVL(tree[mid].r,line+count+);
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i;
for(i=;i<n;i++){
scanf("%d %d",&tree[i].l,&tree[i].r);
}
for(i=;i<n;i++){
scanf("%d",&line[i]);
}
sort(line,line+n); /*for(i=0;i<n;i++){
cout<<i<<" "<<line[i]<<endl;
}*/ BuildAVL(,line); //cout<<":"<<" "<<tree[0].v<<endl; queue<int> q;
int cur;
q.push();
printf("%d",tree[].v);
while(!q.empty()){
cur=q.front();
q.pop();
//cout<<"cur: "<<cur<<endl; if(tree[cur].l!=-){
q.push(tree[cur].l);
printf(" %d",tree[tree[cur].l].v);
}
if(tree[cur].r!=-){
q.push(tree[cur].r);
printf(" %d",tree[tree[cur].r].v);
}
}
printf("\n");
return ;
}
最新文章
- <;<;<; html图片背景平铺
- MySql 里的IFNULL、NULLIF和ISNULL用法区别
- JavaScript -- 小试牛刀
- 十分钟了解分布式计算:GraphX
- Scala入门之Array
- 作业成绩 final 20161124-1201 09:00
- Tmux 常用命令与快捷键
- phalcon: 官方多模块
- css1-颜色和长度
- MySQL优化四(优化表结构)
- PE格式第八讲,TLS表(线程局部存储)
- Linux - Bash shell的功能;内建命令type
- Linux安装网易云音乐
- mycat 安装 分表 分库 读写分离
- 【翻译】Neural Collaborative Filtering--神经协同过滤
- 设置ansible与windows连通性
- url 编码和解码网址
- 项目的整体框架,以及Topology的设计
- Container(容器)与 Injector(注入)
- Beta冲刺1.0
热门文章
- Spring, Hibernate and Oracle Stored Procedures
- Sequence Models 笔记(二)
- 10个C语言经典
- #410div2D. Mike and distribution
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
- C++中栈结构建立和操作
- VSCode编写C/C++项目
- C++ 从内存的角度,学习虚继承机制
- 小小c#算法题 - 6 - 快速排序 (QuickSort)
- 如何保持blog的高质量(相对于自己的进步而言的)