PAT(Advanced)1043 Is It a Binary Search Tree C++实现
PAT(Advanced)甲级1043 Is It a Binary Search Tree C++实现
题目链接
PAT(Advanced)1043 Is It a Binary Search Tree
题目大意
给定整数序列,判断是否是二叉搜索树的前序遍历序列或者二叉搜索树的镜像的前序遍历序列,若是输出“YES”,并输出二叉搜索树或二叉搜索树镜像的后续遍历序列,二叉搜索树的镜像即为将二叉搜索树的左右子树交换,递归地,左右子树的左右子树也必须交换。
算法思路
利用给定的整数序列,构建二叉搜索树,先序遍历判断是否与给定序列一致,若一致则返回true
,否则将二叉搜索树左右子树交换,再次先序遍历判断是否与给定序列一致,若一致返回true
,否则返回false
,递归地进行先序遍历,对于当前子树,子树当前结点关键字与给定序列对应的关键字一致,左右子树中所有关键字必须满足条件,才返回true
,即return (p->val == sequence[number++]) && preOrderTraverse(p->left) && preOrderTraverse(p->right);
AC代码
/*
author : eclipse
email : eclipsecs@qq.com
time : Sun Jun 21 11:09:17 2020
*/
#include <bits/stdc++.h>
using namespace std;struct Node {int val;Node *left;Node *right;Node(int data) : val(data), left(NULL), right(NULL) {}
};class BST {
private:vector<int> sequence;vector<int> postOrderSequence;Node *root;int number;bool preOrderTraverse(Node *p);void inOrderInverse(Node *p);void postOrderTraverse(Node *p);
public:BST(vector<int> v);void insert(Node* &p, int data);bool isBST();void postOrderTraverse();void printPostOrderSequence();
};BST::BST(vector<int> v) {sequence.insert(sequence.begin(), v.begin(), v.end());root = NULL;number = 0;for (int i = 0; i < sequence.size(); i++) {insert(root, sequence[i]);}
}void BST::insert(Node* &p, int data) {if (p == NULL) {p = new Node(data);return;}if (data < p->val) {insert(p->left, data);} else {insert(p->right, data);}
}bool BST::isBST() {if (preOrderTraverse(root)) {return true;} else {number = 0;inOrderInverse(root);return preOrderTraverse(root);}
}bool BST::preOrderTraverse(Node *p) {if (!p) {return true;}return (p->val == sequence[number++]) && preOrderTraverse(p->left) && preOrderTraverse(p->right);
}void BST::inOrderInverse(Node *p) {if (!p) {return;}swap(p->left, p->right);inOrderInverse(p->left);inOrderInverse(p->right);
}void BST::postOrderTraverse(Node *p) {if (!p) {return;}postOrderTraverse(p->left);postOrderTraverse(p->right);postOrderSequence.push_back(p->val);
}void BST::printPostOrderSequence() {for (int i = 0; i < postOrderSequence.size(); i++) {if (i != postOrderSequence.size() - 1) {printf("%d ", postOrderSequence[i]);} else {printf("%d", postOrderSequence[i]);}}
}void BST::postOrderTraverse() {postOrderTraverse(root);
}int main(int argc, char const *argv[]) {int N;scanf("%d", &N);vector<int> v;for (int i = 0; i < N; i++) {int value;scanf("%d", &value);v.push_back(value);}BST *bst = new BST(v);if (bst->isBST()) {printf("YES\n");bst->postOrderTraverse();bst->printPostOrderSequence();} else {printf("NO");}return 0;
}
样例输入1
7
8 6 5 7 10 8 11
样例输出1
YES
5 7 6 8 11 10 8
样例输入2
7
8 10 11 8 6 7 5
样例输出2
YES
11 8 10 7 5 6 8
样例输入3
7
8 6 8 5 10 9 11
样例输出3
NO
鸣谢
PAT
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
PAT(Advanced)1043 Is It a Binary Search Tree C++实现
PAT(Advanced)甲级1043 Is It a Binary Search Tree C++实现
题目链接
PAT(Advanced)1043 Is It a Binary Search Tree
题目大意
给定整数序列,判断是否是二叉搜索树的前序遍历序列或者二叉搜索树的镜像的前序遍历序列,若是输出“YES”,并输出二叉搜索树或二叉搜索树镜像的后续遍历序列,二叉搜索树的镜像即为将二叉搜索树的左右子树交换,递归地,左右子树的左右子树也必须交换。
算法思路
利用给定的整数序列,构建二叉搜索树,先序遍历判断是否与给定序列一致,若一致则返回true
,否则将二叉搜索树左右子树交换,再次先序遍历判断是否与给定序列一致,若一致返回true
,否则返回false
,递归地进行先序遍历,对于当前子树,子树当前结点关键字与给定序列对应的关键字一致,左右子树中所有关键字必须满足条件,才返回true
,即return (p->val == sequence[number++]) && preOrderTraverse(p->left) && preOrderTraverse(p->right);
AC代码
/*
author : eclipse
email : eclipsecs@qq.com
time : Sun Jun 21 11:09:17 2020
*/
#include <bits/stdc++.h>
using namespace std;struct Node {int val;Node *left;Node *right;Node(int data) : val(data), left(NULL), right(NULL) {}
};class BST {
private:vector<int> sequence;vector<int> postOrderSequence;Node *root;int number;bool preOrderTraverse(Node *p);void inOrderInverse(Node *p);void postOrderTraverse(Node *p);
public:BST(vector<int> v);void insert(Node* &p, int data);bool isBST();void postOrderTraverse();void printPostOrderSequence();
};BST::BST(vector<int> v) {sequence.insert(sequence.begin(), v.begin(), v.end());root = NULL;number = 0;for (int i = 0; i < sequence.size(); i++) {insert(root, sequence[i]);}
}void BST::insert(Node* &p, int data) {if (p == NULL) {p = new Node(data);return;}if (data < p->val) {insert(p->left, data);} else {insert(p->right, data);}
}bool BST::isBST() {if (preOrderTraverse(root)) {return true;} else {number = 0;inOrderInverse(root);return preOrderTraverse(root);}
}bool BST::preOrderTraverse(Node *p) {if (!p) {return true;}return (p->val == sequence[number++]) && preOrderTraverse(p->left) && preOrderTraverse(p->right);
}void BST::inOrderInverse(Node *p) {if (!p) {return;}swap(p->left, p->right);inOrderInverse(p->left);inOrderInverse(p->right);
}void BST::postOrderTraverse(Node *p) {if (!p) {return;}postOrderTraverse(p->left);postOrderTraverse(p->right);postOrderSequence.push_back(p->val);
}void BST::printPostOrderSequence() {for (int i = 0; i < postOrderSequence.size(); i++) {if (i != postOrderSequence.size() - 1) {printf("%d ", postOrderSequence[i]);} else {printf("%d", postOrderSequence[i]);}}
}void BST::postOrderTraverse() {postOrderTraverse(root);
}int main(int argc, char const *argv[]) {int N;scanf("%d", &N);vector<int> v;for (int i = 0; i < N; i++) {int value;scanf("%d", &value);v.push_back(value);}BST *bst = new BST(v);if (bst->isBST()) {printf("YES\n");bst->postOrderTraverse();bst->printPostOrderSequence();} else {printf("NO");}return 0;
}
样例输入1
7
8 6 5 7 10 8 11
样例输出1
YES
5 7 6 8 11 10 8
样例输入2
7
8 10 11 8 6 7 5
样例输出2
YES
11 8 10 7 5 6 8
样例输入3
7
8 6 8 5 10 9 11
样例输出3
NO
鸣谢
PAT
最后
- 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!
发布评论