PAT_1043. Is It a Binary Search Tree
来源:互联网 发布:mac视频字幕制作软件 编辑:程序博客网 时间:2024/06/05 08:03
//// main.cpp// PAT_1043. Is It a Binary Search Tree//// Created by wjq on 17/5/9.// Copyright © 2017年 wjq. All rights reserved.//#include <iostream>using namespace std;struct Node{ Node *left,*right; int weight; Node(int w):left(NULL),right(NULL),weight(w){}};int N;//seq用来保存输入,preorder1是正常的先根遍历(根左右),preorder2是根右左遍历,.int seq[1005],preorder1[1005],preorder2[1005],postorder1[1005],postorder2[1005],p1=0,p2=0,p3=0,p4=0;//root保存我们建立的二叉搜索树根节点的地址.Node *root=NULL;void preOrder1(Node *now){ preorder1[p1++]=now->weight; if(now->left!=NULL) preOrder1(now->left); if(now->right!=NULL) preOrder1(now->right);}void preOrder2(Node *now){ preorder2[p2++]=now->weight; if(now->right!=NULL) preOrder2(now->right); if(now->left!=NULL) preOrder2(now->left);}void postOrder1(Node *now){ if(now->left!=NULL) postOrder1(now->left); if(now->right!=NULL) postOrder1(now->right); postorder1[p3++]=now->weight;}void postOrder2(Node *now){ if(now->right!=NULL) postOrder2(now->right); if(now->left!=NULL) postOrder2(now->left); postorder2[p4++]=now->weight;}void insert(Node *&now ,int num) //*&now表示的是main函数中root指针变量的别名{ if(now==NULL) { now=new Node(num); return; //这里如果不加return 会导致死循环递归,因为此时now->weight=num; } if(now->weight>num) insert(now->left,num); else insert(now->right,num);}bool judge(int a[]){ for(int i=0;i<N;i++) if(seq[i]!=a[i]) return false; return true;}int main(){ cin>>N; for(int i=0;i<N;i++) { cin>>seq[i]; insert(root,seq[i]); } preOrder1(root); preOrder2(root); if(judge(preorder1)) { postOrder1(root); cout<<"YES"<<endl; cout<<postorder1[0]; for(int i=1;i<N;i++) cout<<" "<<postorder1[i]; } else if(judge(preorder2)) { postOrder2(root); cout<<"YES"<<endl; cout<<postorder2[0]; for(int i=1;i<N;i++) cout<<" "<<postorder2[i]; } else cout<<"NO"<<endl;}
题意:
给你一个先根遍历的序列,判断这个序列是否能够作为BST树或BST树的镜像的先根遍历.
BST树的镜像就是BST树的每一个节点的左右子树都要对调.
思路:
1.先通过题目给出的序列构造BST.
2.求出该BST的先根遍历序列和BST镜像的先根遍历序列,求BST镜像的先根遍历序列只需要遵循 根->右子树->左子树 的遍历顺序即可.
3.将2中求出的两个序列与题目中给出的序列进行对比.然后输出后根遍历序列.
遇到的bug:在写insert函数(构建BST)的时候传入的指针是形参,在函数中对形参(指针或普通变量)做改变不会影响到函数外的指针或变量,因此我的root始终指向null,每个节点之间的关联也没有建立起来.
解决方式:用 node *&now(传引用)的形式写函数的参数,now是指针的引用,理解为指针的别名,在函数内对now改变会影响到函数外的指针的值.
小tips:
int* &p;
记忆方式:p与&靠近,所以p是一个引用(某个变量的别名),这个引用的类型是int*,所以p是对某个指针的引用.
int& *p;
错误的.
0 0
- PAT_1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043.Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- Is It a Binary Search Tree (25)
- PAT A 1043. Is It a Binary Search Tree (25)
- PAT-A-1043. Is It a Binary Search Tree (25)
- pat-a 1043. Is It a Binary Search Tree (25)
- 1043. Is It a Binary Search Tree (25)
- Nginx有哪些有趣的玩法?
- Unity使用Socket与后台连接,包含Json的读写
- 划分dp,区间差最小
- 解决 win10 pycurl安装出错 Command "python setup.py egg_info" failed with error code 10
- 利用rsyslog 配置用户行为日志审计
- PAT_1043. Is It a Binary Search Tree
- 剑指offer 21. 包含min函数的栈
- Mybatis中接口和对应的mapper文件位置配置详解
- c语言:随机猜数字游戏
- Java 网络编程总结
- 用Imageview实现复选框效果
- 数据库事务隔离级别及相关实例
- 学习途径IOS
- 面向对象的特征有哪些方面