PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
題目描述:
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.
譯:二叉搜索樹(BST)遞歸定義為具有以下屬性的二叉樹:
- 節點的左子樹只包含鍵值小于該節點鍵值的節點。
- 節點的右子樹只包含鍵值大于或等于該節點鍵的節點。
- 左子樹和右子樹都必須是二叉搜索樹。
如果我們交換每個節點的左子樹和右子樹,那么得到的樹被稱為一個BST的鏡像。現在給定一個整數鍵序列,您應該知道它是BST的預序遍歷序列還是BST的鏡像。
Input Specification (輸入說明):
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.
譯:每個輸入文件包含一個測試用例。對于每種情況,第一行包含一個正整數N(≤1000)。然后在下一行給出N個整數鍵。一行中的所有數字用空格隔開。
Output Specification (輸出說明):
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.
譯:對于每個測試用例,如果序列是BST的預序遍歷序列或BST的鏡像,則首先在一行中打印 YES,如果不是,則打印 NO。然后,如果答案是YES,則在下一行打印該樹的 postorder 遍歷序列。一行中的所有數字必須用空格隔開,行尾不能有多余的空格。。
Sample Input 1 (樣例輸入1):
7 8 6 5 7 10 8 11Sample Output 1 (樣例輸出1):
YES 5 7 6 8 11 10 8Sample Input 2 (樣例輸入2):
7 8 10 11 8 6 7 5Sample Output 2 (樣例輸出2):
YES 11 8 10 7 5 6 8Sample Input 3 (樣例輸入3):
7 8 6 8 5 10 9 11Sample Output 3 (樣例輸出3):
NOThe Idea:
技巧如下:
- 靜像樹的先序遍歷,只需交換 排序二叉樹訪問 左子樹 和 右子樹的 順序.
- 靜像樹的后序遍歷,只需交換 排序二叉樹訪問 左子樹 和 右子樹的 順序
- 利用字符串記錄下輸入數字的序列、排序二叉樹的先序序列、鏡像樹的先序序列,方便比較。
The Codes:
#include<bits/stdc++.h> using namespace std ; // 排序二叉樹的存儲結構 struct node{int data ; // 數據域 node* lchild ; // 指向左孩子的指針域 node* rchild ; // 指向右孩子的指針域 }; int n , t ; string s1="" , pre="" , mpre=""; vector<int> ans ; // 存儲答案的后序序列 // 創建一個新結點 node* newNode(int x){node* root = new node ;root -> lchild = root ->rchild = NULL ; // 新結點的左右孩子指針域均為空 root -> data = x ; // 新結點的數據域為 x return root ; // 放回新結點 } void insert(node* &root , int x){if(root == NULL){root = newNode(x) ; // 如果為空樹,即插入位置 return ;}if(x < root -> data) insert(root -> lchild , x) ; // 說明 x 在左子樹,遞歸左子樹 else insert(root -> rchild , x) ; // 說明 x 在右子樹,遞歸右子樹 } // 對二叉排序樹先序遍歷 void preorder(node* root){if(root == NULL) return ; // 空樹返回 pre += to_string(root -> data) ; // 訪問當前根結點 preorder(root->lchild) ; // 訪問左子樹 preorder(root->rchild) ; // 訪問右子樹 } // 對鏡像樹先序遍歷 void mpreorder(node* root){if(root == NULL) return ; // 空樹返回 mpre += to_string(root -> data) ;// 訪問當前根結點 mpreorder(root->rchild) ; // 訪問左子樹 (排序二叉樹的右子樹就是鏡像樹的左子樹)mpreorder(root->lchild) ; // 訪問右子樹 (排序二叉樹的左子樹就是鏡像樹的右子樹) } // 后序遍歷二叉排序樹 void postorder(node* root){if(root == NULL) return ;// 空樹返回 postorder(root->lchild) ;// 訪問左子樹 postorder(root->rchild) ;// 訪問右子樹 ans.push_back(root -> data) ;// 訪問當前根結點 } // 后序遍歷鏡像樹 void mpostorder(node* root){if(root == NULL) return ;// 空樹返回 mpostorder(root->rchild) ; // 訪問左子樹 (排序二叉樹的右子樹就是鏡像樹的左子樹)mpostorder(root->lchild) ; // 訪問右子樹 (排序二叉樹的左子樹就是鏡像樹的右子樹)ans.push_back(root -> data) ; // 訪問當前根結點 } int main(){scanf("%d" , &n) ; // 輸入結點個數 n node* root = NULL ; // 新建根結點,初值為 NULL for(int i = 0 ; i < n ; i ++){scanf("%d" , &t) ; // 輸入第 i 個結點的權值 insert(root , t) ; // 往排序二叉樹中插入該結點 s1 += to_string(t) ; // 記錄輸入數據的順序的string,便于比較 }preorder(root) ; // 排序二叉樹的先序遍歷 mpreorder(root) ; // 靜態樹的先序遍歷 if(s1 == pre ){ // 如果輸入序列即為排序二叉樹的先序序列 postorder(root) ;// 后序遍歷排序二叉樹 printf("YES\n") ; for(int i = 0 ; i < ans.size() ; i ++) // 打印后序序列 printf("%d%c" , ans[i] , (i == ans.size() - 1)?'\n':' ') ;}else if(s1 == mpre){// 如果輸入序列為鏡像樹的先序序列 mpostorder(root) ; // 后序遍歷鏡像樹 printf("YES\n") ;for(int i = 0 ; i < ans.size() ; i ++) // 打印后序序列 printf("%d%c" , ans[i] , (i == ans.size() - 1)?'\n':' ') ;}else{ // 不滿足要求的序列 printf("NO\n") ;}return 0; }總結
以上是生活随笔為你收集整理的PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vux使用Swiper遇到的问题
- 下一篇: 弘辽科技:如何制定淘宝店铺推广计划?店铺