MMT12. 二叉搜索树判定
描述
给定一个二叉树,判断其是否是一个有效的二叉搜索树。输入描述
第一行两个数n,root,分别表示二叉树有n个节点,第root个节点时二叉树的根输出描述
输出"true"如果给定二叉树是二叉搜索树,否则输出"false"示例1
输入:
5 1 5 2 3 1 0 0 3 4 5 4 0 0 6 0 0
输出:
false
示例2
输入:
5 1 2 2 3 1 0 0 4 4 5 3 0 0 6 0 0
输出:
true
C++ 解法, 执行用时: 25ms, 内存消耗: 6676KB, 提交时间: 2020-07-24
#include <bits/stdc++.h> using namespace std; struct Node { int val; int left; int right; }; int n,root; vector<Node> vv; vector<int> ans; void inorder(int r) { if(!r) return; inorder(vv[r].left); ans.push_back(vv[r].val); inorder(vv[r].right); } bool check() { int pre = -0x7fffffff; for(int i = 0; i < ans.size(); ++i) { if(ans[i] <= pre) return false; pre = ans[i]; } return true; } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>root; vv.resize(n+1); int v,l,r; for(int i = 1; i <= n; ++i) cin>>vv[i].val>>vv[i].left>>vv[i].right; inorder(root); if(check()) cout << "true" << endl; else cout << "false" << endl; }
C++ 解法, 执行用时: 26ms, 内存消耗: 5800KB, 提交时间: 2021-09-09
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* pre; bool isBinarySearchTree(TreeNode * root) { if(root == nullptr) { return true; } bool left = isBinarySearchTree(root->left); if(pre != nullptr && pre->val >= root->val) { return false; } pre = root; bool right = isBinarySearchTree(root->right); return left && right; } int main(void) { int n,r; scanf("%d%d",&n,&r); TreeNode * tree, * root; tree = (TreeNode*)malloc(sizeof(TreeNode)*(n+1)); root = &tree[r]; for (int i=1;i<=n;i++) { int v,l,r; scanf("%d%d%d",&v,&l,&r); tree[i].val = v; tree[i].left = l?&tree[l]:0; tree[i].right = r?&tree[r]:0; } printf(isBinarySearchTree(root)?"true\n":"false\n"); return 0; }
C++ 解法, 执行用时: 26ms, 内存消耗: 7472KB, 提交时间: 2021-09-16
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; bool isBinarySearchTree(TreeNode* root, int min, int max) { if (root == NULL) return true; bool self = root->val <= max && root->val >= min; bool left = isBinarySearchTree(root->left, min, root->val); bool right = isBinarySearchTree(root->right, root->val, max); return self && left && right; } int main(void) { int n, r; scanf("%d%d", &n, &r); TreeNode* tree, * root; tree = (TreeNode*)malloc(sizeof(TreeNode) * (n + 1)); root = &tree[r]; for (int i = 1; i <= n; i++) { int v, l, r; scanf("%d%d%d", &v, &l, &r); tree[i].val = v; tree[i].left = l ? &tree[l] : 0; tree[i].right = r ? &tree[r] : 0; } printf(isBinarySearchTree(root, INT_MIN, INT_MAX) ? "true\n" : "false\n"); return 0; }
C++ 解法, 执行用时: 30ms, 内存消耗: 7380KB, 提交时间: 2020-10-31
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; bool isBinarySearchTree(TreeNode * root,int min,int max) { if(root==NULL) return true; bool self = root->val<=max && root->val>=min; bool left = isBinarySearchTree(root->left,min,root->val); bool right = isBinarySearchTree(root->right,root->val,max); return self && left && right; } int main(void) { int n,r; scanf("%d%d",&n,&r); TreeNode * tree, * root; tree = (TreeNode*)malloc(sizeof(TreeNode)*(n+1)); root = &tree[r]; for (int i=1;i<=n;i++) { int v,l,r; scanf("%d%d%d",&v,&l,&r); tree[i].val = v; tree[i].left = l?&tree[l]:0; tree[i].right = r?&tree[r]:0; } printf(isBinarySearchTree(root,INT_MIN,INT_MAX)?"true\n":"false\n"); return 0; }
C++ 解法, 执行用时: 30ms, 内存消耗: 7416KB, 提交时间: 2020-11-01
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; bool isBinarySearchTree(TreeNode * root,int min,int max) { if(root==NULL) return true; bool self = root->val<=max && root->val>=min; bool left = isBinarySearchTree(root->left,min,root->val); bool right = isBinarySearchTree(root->right,root->val,max); return self && left && right; } int main(void) { int n,r; scanf("%d%d",&n,&r); TreeNode * tree, * root; tree = (TreeNode*)malloc(sizeof(TreeNode)*(n+1)); root = &tree[r]; for (int i=1;i<=n;i++) { int v,l,r; scanf("%d%d%d",&v,&l,&r); tree[i].val = v; tree[i].left = l?&tree[l]:0; tree[i].right = r?&tree[r]:0; } printf(isBinarySearchTree(root,INT_MIN,INT_MAX)?"true\n":"false\n"); return 0; }