PAT A 1115. Counting Nodes in a BST (30)
2017-08-21 23:08
495 查看
只要是树的题都不会很难,简单的建立排序树,再层次遍历。
测试点5通不过是因为未考虑只有一个结点的情况。
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <queue>
using namespace std;
struct node{
int key;
node *lchild,*rchild;
};
int keys[1002]={0};
int levelnode[1002]={0};
queue<node*> Q;
void create_bst(node *&T,const int &i,const int &n){
if(i<n){
if(T==NULL){
T=(node *)malloc(sizeof(node));
T->key=keys[i];
T->lchild=T->rchild=NULL;
}
else if(keys[i]<=T->key)create_bst(T->lchild,i,n);
else if(keys[i]>T->key)create_bst(T->rchild,i,n);
}
}
int bfs(node *T){
int level=0;
Q.push(T);
node *rear=T;
while(!Q.empty()){
node *tmp=Q.front();
Q.pop();
++levelnode[level];
if(tmp->lchild!=NULL)Q.push(tmp->lchild);
if(tmp->rchild!=NULL)Q.push(tmp->rchild);
if(rear==tmp){//最好使用指向节点的指针来判断是否进入下一层,而不要使用关键字相等来判断,因为题中很明显会出现相关的关键字,
++level;//从而可以避免莫名其妙的BUG,当然这个题不会有这种情况。
rear=Q.back();
}
}
return level-1;
}
int main(void){
//freopen("in.log","r",stdin);
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)scanf("%d",&keys[i]);
node *T=NULL;
for(int i=0;i<n;++i)create_bst(T,i,n);
int level=bfs(T);
printf("%d + %d = %d\n",levelnode[level],levelnode[level-1],levelnode[level]+levelnode[level-1]);
return 0;
}
测试点5通不过是因为未考虑只有一个结点的情况。
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <queue>
using namespace std;
struct node{
int key;
node *lchild,*rchild;
};
int keys[1002]={0};
int levelnode[1002]={0};
queue<node*> Q;
void create_bst(node *&T,const int &i,const int &n){
if(i<n){
if(T==NULL){
T=(node *)malloc(sizeof(node));
T->key=keys[i];
T->lchild=T->rchild=NULL;
}
else if(keys[i]<=T->key)create_bst(T->lchild,i,n);
else if(keys[i]>T->key)create_bst(T->rchild,i,n);
}
}
int bfs(node *T){
int level=0;
Q.push(T);
node *rear=T;
while(!Q.empty()){
node *tmp=Q.front();
Q.pop();
++levelnode[level];
if(tmp->lchild!=NULL)Q.push(tmp->lchild);
if(tmp->rchild!=NULL)Q.push(tmp->rchild);
if(rear==tmp){//最好使用指向节点的指针来判断是否进入下一层,而不要使用关键字相等来判断,因为题中很明显会出现相关的关键字,
++level;//从而可以避免莫名其妙的BUG,当然这个题不会有这种情况。
rear=Q.back();
}
}
return level-1;
}
int main(void){
//freopen("in.log","r",stdin);
int n;
scanf("%d",&n);
for(int i=0;i<n;++i)scanf("%d",&keys[i]);
node *T=NULL;
for(int i=0;i<n;++i)create_bst(T,i,n);
int level=bfs(T);
printf("%d + %d = %d\n",levelnode[level],levelnode[level-1],levelnode[level]+levelnode[level-1]);
return 0;
}
相关文章推荐
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
- *1115. Counting Nodes in a BST (30) <二叉搜索树>
- PAT甲级 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30) 二叉树
- 1115. Counting Nodes in a BST (30)
- PAT 1115. Counting Nodes in a BST (30) 建树
- 1115. Counting Nodes in a BST (30)
- PAT (Advanced Level) Practise 1115 Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
- PAT 甲级 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- PAT 1115 Counting Nodes in a BST (30)
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30) 解题报告
- 1115. Counting Nodes in a BST (30)解题报告
- PAT (Advanced Level)1115. Counting Nodes in a BST (30)
- PAT (Advanced Level) Practise 1115 Counting Nodes in a BST (30)