给定大小为n的数组,任务是查找数组是否可以表示具有n个级别的BST。
由于级别为n,因此我们以以下方式构造树。
假设数字X
- 高于X的数字在右侧
- 低于X的数字在左侧。
注意:在插入过程中,我们永远不会超出已经访问过的号码。
例子:
Input : 500, 200, 90, 250, 100
Output : No
Input : 5123, 3300, 783, 1111, 890
Output : Yes
解释 :
对于序列500、200、90、250、100,形成的树(如上图所示)不能表示BST。
序列5123、3300、783、1111、890形成二叉搜索树,因此是正确的序列。
方法1:通过构造BST
我们首先将所有数组值逐级插入到Tree中。作为插入,我们检查当前值是否小于或等于先前的值。构造完树之后,我们检查构造的树是否为Binary Search Tree。
C++
// C++ program to Check given array
// can represent BST or not
#include
using namespace std;
// structure for Binary Node
struct Node {
int key;
struct Node *right, *left;
};
Node* newNode(int num)
{
Node* temp = new Node;
temp->key = num;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// To create a Tree with n levels. We always
// insert new node to left if it is less than
// previous value.
Node* createNLevelTree(int arr[], int n)
{
Node* root = newNode(arr[0]);
Node* temp = root;
for (int i = 1; i < n; i++) {
if (temp->key > arr[i]) {
temp->left = newNode(arr[i]);
temp = temp->left;
}
else {
temp->right = newNode(arr[i]);
temp = temp->right;
}
}
return root;
}
// Please refer below post for details of this
// function.
// https:// www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
bool isBST(Node* root, int min, int max)
{
if (root == NULL)
return true;
if (root->key < min || root->key > max)
return false;
// Allow only distinct values
return (isBST(root->left, min,
(root->key) - 1)
&& isBST(root->right,
(root->key) + 1, max));
}
// Returns tree if given array of size n can
// represent a BST of n levels.
bool canRepresentNLevelBST(int arr[], int n)
{
Node* root = createNLevelTree(arr, n);
return isBST(root, INT_MIN, INT_MAX);
}
// Driver code
int main()
{
int arr[] = { 512, 330, 78, 11, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
if (canRepresentNLevelBST(arr, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to Check given array
// can represent BST or not
class GFG
{
// structure for Binary Node
static class Node
{
int key;
Node right, left;
};
static Node newNode(int num)
{
Node temp = new Node();
temp.key = num;
temp.left = null;
temp.right = null;
return temp;
}
// To create a Tree with n levels. We always
// insert new node to left if it is less than
// previous value.
static Node createNLevelTree(int arr[], int n)
{
Node root = newNode(arr[0]);
Node temp = root;
for (int i = 1; i < n; i++)
{
if (temp.key > arr[i])
{
temp.left = newNode(arr[i]);
temp = temp.left;
}
else
{
temp.right = newNode(arr[i]);
temp = temp.right;
}
}
return root;
}
// Please refer below post for details of this
// function.
// https:// www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
static boolean isBST(Node root, int min, int max)
{
if (root == null)
{
return true;
}
if (root.key < min || root.key > max)
{
return false;
}
// Allow only distinct values
return (isBST(root.left, min,
(root.key) - 1)
&& isBST(root.right,
(root.key) + 1, max));
}
// Returns tree if given array of size n can
// represent a BST of n levels.
static boolean canRepresentNLevelBST(int arr[], int n)
{
Node root = createNLevelTree(arr, n);
return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
// Driver code
public static void main(String[] args)
{
int arr[] = {512, 330, 78, 11, 8};
int n = arr.length;
if (canRepresentNLevelBST(arr, n))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
/* This code contributed by PrinciRaj1992 */
Python
# Python program to Check given array
# can represent BST or not
# A binary tree node has data,
# left child and right child
class newNode():
def __init__(self, data):
self.key = data
self.left = None
self.right = None
# To create a Tree with n levels. We always
# insert new node to left if it is less than
# previous value.
def createNLevelTree(arr, n):
root = newNode(arr[0])
temp = root
for i in range(1, n):
if (temp.key > arr[i]):
temp.left = newNode(arr[i])
temp = temp.left
else:
temp.right = newNode(arr[i])
temp = temp.right
return root
# Please refer below post for details of this
# function.
# https:# www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
def isBST(root, min, max):
if (root == None):
return True
if (root.key < min or root.key > max):
return False
# Allow only distinct values
return (isBST(root.left, min, (root.key) - 1) and
isBST(root.right,(root.key) + 1, max))
# Returns tree if given array of size n can
# represent a BST of n levels.
def canRepresentNLevelBST(arr, n):
root = createNLevelTree(arr, n)
return isBST(root, 0, 2**32)
# Driver code
arr = [512, 330, 78, 11, 8]
n = len(arr)
if (canRepresentNLevelBST(arr, n)):
print("Yes")
else:
print("No")
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to Check given array
// can represent BST or not
using System;
class GFG
{
// structure for Binary Node
public class Node
{
public int key;
public Node right, left;
};
static Node newNode(int num)
{
Node temp = new Node();
temp.key = num;
temp.left = null;
temp.right = null;
return temp;
}
// To create a Tree with n levels. We always
// insert new node to left if it is less than
// previous value.
static Node createNLevelTree(int []arr, int n)
{
Node root = newNode(arr[0]);
Node temp = root;
for (int i = 1; i < n; i++)
{
if (temp.key > arr[i])
{
temp.left = newNode(arr[i]);
temp = temp.left;
}
else
{
temp.right = newNode(arr[i]);
temp = temp.right;
}
}
return root;
}
// Please refer below post for details of this
// function.
// https:// www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/
static bool isBST(Node root, int min, int max)
{
if (root == null)
{
return true;
}
if (root.key < min || root.key > max)
{
return false;
}
// Allow only distinct values
return (isBST(root.left, min,
(root.key) - 1) &&
isBST(root.right,
(root.key) + 1, max));
}
// Returns tree if given array of size n can
// represent a BST of n levels.
static bool canRepresentNLevelBST(int []arr, int n)
{
Node root = createNLevelTree(arr, n);
return isBST(root, int.MinValue, int.MaxValue);
}
// Driver code
public static void Main(String[] args)
{
int []arr = {512, 330, 78, 11, 8};
int n = arr.Length;
if (canRepresentNLevelBST(arr, n))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code contributed by Rajput-Ji
C++
// C++ program to Check given array
// can represent BST or not
#include
using namespace std;
// Driver code
int main()
{
int arr[] = { 5123, 3300, 783, 1111, 890 };
int n = sizeof(arr) / sizeof(arr[0]);
int max = INT_MAX;
int min = INT_MIN;
bool flag = true;
for (int i = 1; i < n; i++) {
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max) {
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max) {
// min remains same, update max
max = arr[i - 1];
}
else {
flag = false;
break;
}
}
if (flag) {
cout << "Yes";
}
else {
// if the loop completed successfully without encountering else condition
cout << "No";
}
return 0;
}
Java
// Java program to Check given array
// can represent BST or not
class Solution
{
// Driver code
public static void main(String args[])
{
int arr[] = { 5123, 3300, 783, 1111, 890 };
int n = arr.length;
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
boolean flag = true;
for (int i = 1; i < n; i++) {
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max) {
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max) {
// min remains same, update max
max = arr[i - 1];
}
else {
flag = false;
break;
}
}
if (flag) {
System.out.println("Yes");
}
else {
// if the loop completed successfully without encountering else condition
System.out.println("No");
}
}
}
//contributed by Arnab Kundu
Python3
# Python3 program to Check given array
# can represent BST or not
# Driver Code
if __name__ == '__main__':
arr = [5123, 3300, 783, 1111, 890]
n = len(arr)
max = 2147483647 # INT_MAX
min = -2147483648 # INT_MIN
flag = True
for i in range(1,n):
# This element can be inserted to the
# right of the previous element, only
# if it is greater than the previous
# element and in the range.
if (arr[i] > arr[i - 1] and
arr[i] > min and arr[i] < max):
# max remains same, update min
min = arr[i - 1]
# This element can be inserted to the
# left of the previous element, only
# if it is lesser than the previous
# element and in the range.
elif (arr[i] < arr[i - 1] and
arr[i] > min and arr[i] < max):
# min remains same, update max
max = arr[i - 1]
else :
flag = False
break
if (flag):
print("Yes")
else:
# if the loop completed successfully
# without encountering else condition
print("No")
# This code is contributed
# by SHUBHAMSINGH10
C#
using System;
// C# program to Check given array
// can represent BST or not
public class Solution
{
// Driver code
public static void Main(string[] args)
{
int[] arr = new int[] {5123, 3300, 783, 1111, 890};
int n = arr.Length;
int max = int.MaxValue;
int min = int.MinValue;
bool flag = true;
for (int i = 1; i < n; i++)
{
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max)
{
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max)
{
// min remains same, update max
max = arr[i - 1];
}
else
{
flag = false;
break;
}
}
if (flag)
{
Console.WriteLine("Yes");
}
else
{
// if the loop completed successfully without encountering else condition
Console.WriteLine("No");
}
}
}
// This code is contributed by Shrikant13
输出:
Yes
方法2(基于数组)
1.取两个变量max = INT_MAX标记左子树的最大限制,min = INT_MIN标记右子树的最小限制。
2.从arr [1]循环到arr [n-1]
3.对每个元素进行检查
一种。如果(arr [i]> arr [i-1] && arr [i]> min && arr [i]
C++
// C++ program to Check given array
// can represent BST or not
#include
using namespace std;
// Driver code
int main()
{
int arr[] = { 5123, 3300, 783, 1111, 890 };
int n = sizeof(arr) / sizeof(arr[0]);
int max = INT_MAX;
int min = INT_MIN;
bool flag = true;
for (int i = 1; i < n; i++) {
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max) {
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max) {
// min remains same, update max
max = arr[i - 1];
}
else {
flag = false;
break;
}
}
if (flag) {
cout << "Yes";
}
else {
// if the loop completed successfully without encountering else condition
cout << "No";
}
return 0;
}
Java
// Java program to Check given array
// can represent BST or not
class Solution
{
// Driver code
public static void main(String args[])
{
int arr[] = { 5123, 3300, 783, 1111, 890 };
int n = arr.length;
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
boolean flag = true;
for (int i = 1; i < n; i++) {
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max) {
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max) {
// min remains same, update max
max = arr[i - 1];
}
else {
flag = false;
break;
}
}
if (flag) {
System.out.println("Yes");
}
else {
// if the loop completed successfully without encountering else condition
System.out.println("No");
}
}
}
//contributed by Arnab Kundu
Python3
# Python3 program to Check given array
# can represent BST or not
# Driver Code
if __name__ == '__main__':
arr = [5123, 3300, 783, 1111, 890]
n = len(arr)
max = 2147483647 # INT_MAX
min = -2147483648 # INT_MIN
flag = True
for i in range(1,n):
# This element can be inserted to the
# right of the previous element, only
# if it is greater than the previous
# element and in the range.
if (arr[i] > arr[i - 1] and
arr[i] > min and arr[i] < max):
# max remains same, update min
min = arr[i - 1]
# This element can be inserted to the
# left of the previous element, only
# if it is lesser than the previous
# element and in the range.
elif (arr[i] < arr[i - 1] and
arr[i] > min and arr[i] < max):
# min remains same, update max
max = arr[i - 1]
else :
flag = False
break
if (flag):
print("Yes")
else:
# if the loop completed successfully
# without encountering else condition
print("No")
# This code is contributed
# by SHUBHAMSINGH10
C#
using System;
// C# program to Check given array
// can represent BST or not
public class Solution
{
// Driver code
public static void Main(string[] args)
{
int[] arr = new int[] {5123, 3300, 783, 1111, 890};
int n = arr.Length;
int max = int.MaxValue;
int min = int.MinValue;
bool flag = true;
for (int i = 1; i < n; i++)
{
// This element can be inserted to the right
// of the previous element, only if it is greater
// than the previous element and in the range.
if (arr[i] > arr[i - 1] && arr[i] > min && arr[i] < max)
{
// max remains same, update min
min = arr[i - 1];
}
// This element can be inserted to the left
// of the previous element, only if it is lesser
// than the previous element and in the range.
else if (arr[i] < arr[i - 1] && arr[i] > min && arr[i] < max)
{
// min remains same, update max
max = arr[i - 1];
}
else
{
flag = false;
break;
}
}
if (flag)
{
Console.WriteLine("Yes");
}
else
{
// if the loop completed successfully without encountering else condition
Console.WriteLine("No");
}
}
}
// This code is contributed by Shrikant13
输出:
Yes