1043 Is It a Binary Search Tree
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.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
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.
Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO
题意:
给出一个数字序列,问这个序列是不是一棵BST或者Mirror BST前序遍历的结果,如果是则输出对应树的后序遍历结果,如果不是则输出NO。
思路:
施工中………………
Code:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int n; 6 vector<int> v; 7 vector<int> post; 8 9 typedef struct Node* node; 10 11 struct Node { 12 int val; 13 node left; 14 node right; 15 Node(int v) { 16 val = v; 17 left = NULL; 18 right = NULL; 19 } 20 }; 21 22 bool isBST() { 23 bool found1 = false; 24 for (int i = 1; i < n; ++i) { 25 if (v[i] >= v[0]) found1 = true; 26 if (found1 && v[i] < v[0]) return false; 27 } 28 return true; 29 } 30 31 bool isMIBST() { 32 bool found2 = false; 33 for (int i = 1; i < n; ++i) { 34 if (v[i] < v[0]) found2 = true; 35 if (found2 && v[i] >= v[0]) return false; 36 } 37 return true; 38 } 39 40 node buildTree(int s, int e, bool isBST) { 41 if (s > e) return NULL; 42 node root = new Node(v[s]); 43 int pos = -1; 44 if (isBST) { 45 for (int i = s + 1; i <= e; ++i) 46 if (v[i] >= v[s]) { 47 pos = i; 48 break; 49 } 50 } else { 51 for (int i = s + 1; i <= e; ++i) 52 if (v[i] < v[s]) { 53 pos = i; 54 break; 55 } 56 } 57 if (pos == -1) return root; 58 root->left = buildTree(s + 1, pos - 1, isBST); 59 root->right = buildTree(pos, e, isBST); 60 return root; 61 } 62 63 void postOrder(node root) { 64 if (root == NULL) return; 65 postOrder(root->left); 66 postOrder(root->right); 67 post.push_back(root->val); 68 } 69 70 int main() { 71 cin >> n; 72 v.resize(n + 1); 73 for (int i = 0; i < n; ++i) cin >> v[i]; 74 node root; 75 if (isBST()) { 76 cout << "YES" << endl; 77 root = buildTree(0, n - 1, true); 78 } else if (isMIBST()) { 79 cout << "YES" << endl; 80 root = buildTree(0, n - 1, false); 81 } else { 82 cout << "NO" << endl; 83 return 0; 84 } 85 postOrder(root); 86 bool isFirst = true; 87 for (int i : post) { 88 if (isFirst) { 89 cout << i; 90 isFirst = false; 91 } else { 92 cout << " " << i; 93 } 94 } 95 cout << endl; 96 return 0; 97 }
注意:
上面的代码有三组数据不能通过,有时间再更正…………
相关文章
- Is it correct that a .feature file always maps to a step file?
- Is it possible to get a good stack trace with .NET async methods?
- Is there a way to detect if a browser window is not currently active?
- Warning about memory “Excessive Grant” in the query plan - how to find out what is causing it?
- Is it bad to rely on foreign key cascading? 外键 级联操作
- 1043 Is It a Binary Search Tree (25 分)【难度: 中 / 知识点: 构造二叉搜索树(BST) 】
- 企业IT架构转型之道:阿里巴巴中台战略思想与架构实战. 3.3 阿里巴巴分布式服务框架HSF
- Another app is currently holding the yum lock; waiting for it to exit
- remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
- 《IT运维之道》——3.2 IT运维服务的原则
- And one thing may differ I found is that, it only compatible with python3,so when your default pip installed in python is 2.7 not python3 you need use "python3 -m pip install chefboost" instead
- What Apache ZooKeeper is and when should it be used?
- For 32-bit BOOL is a signed char, whereas under 64-bit it is a bool.
- 解决报错 We‘re sorry but doesn‘t work properly without JavaScript enabled. Please enable it to continue.
- 这种新型EDR工具可实现秒级对全IT资产的检测与修复
- 中小企业IT手册 四种方式提升云安全
- IT必读:OA中如何制作转正申请单
- 为何IT现代化已成国家网络战略中的关键所在
- [已解决]报错: qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it ...
- Another app is currently holding the yum lock; waiting for it to exit.. yum被锁定无法使用
- [LeetCode] 1250. Check If It Is a Good Array 检查好数组
- IDC:云环境中部署IT基础设施日趋流行
- 安全、合规、IT运营面临的5大主要挑战
- 只有IT人才有机会得满分的高考试卷,但99%的程序员都败在第四道文综题上