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.If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
For each test case, first print in a line YES if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or NO if not. Then if the answer is YES, print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
思路:先将输入的数字按照左子树小于根节点建树,然后得到该树的前序序列和该树的镜像的前序序列,如果出现相等,输出对应的后序序列。
#include <cstdio> #include <vector> using namespace std; typedef struct Node { Node* left; Node* right; int v; }node; // 先序遍历一棵二叉树,将其先序序列保存到pre中 void getPreOreder(node* root, vector<int> &pre) { if(root == nullptr) return; pre.push_back(root->v); getPreOreder(root->left, pre); getPreOreder(root->right, pre); } // 将root代表的二叉树的镜像二叉树的先序保存到mirrorPre中 void getMirrorPre(node* root, vector<int> &mirrorPre) { if(root == nullptr) return; mirrorPre.push_back(root->v); getMirrorPre(root->right, mirrorPre); getMirrorPre(root->left, mirrorPre); } // 后序遍历一棵二叉树,将其先序序列保存到pre中 void getPostOrder(node* root, vector<int> &post) { if(root == nullptr) return; getPostOrder(root->left, post); getPostOrder(root->right, post); post.push_back(root->v); } // 将root代表的二叉树的镜像二叉树的后序保存到mirrorPre中 void getMirrorPost(node* root, vector<int> &mirrorPost) { if(root == nullptr) return; getMirrorPost(root->right, mirrorPost); getMirrorPost(root->left, mirrorPost); mirrorPost.push_back(root->v); } void show(vector<int> result) { for(int i = 0; i < result.size(); i++) { if(i != 0) printf(" "); printf("%d", result[i]); } } node* newNode(int v) { node* tem = new Node(); tem->left = tem->right = nullptr; tem->v = v; return tem; } void insert(node* &root, int v) { if(root == nullptr) { root = newNode(v); return; } if(root->v <= v) insert(root->right, v); else insert(root->left, v); } int main() { int n; scanf("%d", &n); vector<int> in; node* root = nullptr; for(int i = 0; i < n; i++) { int tem; scanf("%d", &tem); in.push_back(tem); insert(root, tem); } vector<int> pre; vector<int> mirrorPre; vector<int> result; getPreOreder(root, pre); if(pre == in) { printf("YES\n"); getPostOrder(root, result); show(result); return 0; } getMirrorPre(root, mirrorPre); if(mirrorPre == in) { printf("YES\n"); getMirrorPost(root, result); show(result); return 0; } printf("NO\n"); return 0; }