检查给定的数组是否可以表示二叉搜索树的级别顺序遍历
给定一个大小为n的数组。问题是检查给定的数组是否可以表示二叉搜索树的级别顺序遍历。
例子:
Input : arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10}
Output : Yes
For the given arr[] the Binary Search Tree is:
7
/ \
4 12
/ \ /
3 6 8
/ / \
1 5 10
Input : arr[] = {11, 6, 13, 5, 12, 10}
Output : No
The given arr[] do not represent the level
order traversal of a BST.
这个想法是使用队列数据结构。队列的每个元素都有一个结构,比如NodeDetails ,它存储树节点的详细信息。详细信息是节点的数据,以及两个变量min和max ,其中min存储可以是左子树的一部分的节点值的下限,而max存储可以是右子树的一部分的节点值的上限对于NodeDetails结构变量中的指定节点。对于第一个数组值 arr[0],创建一个NodeDetails结构,其中 arr[0] 作为节点的数据, min = INT_MIN 和max = INT_MAX。将此结构变量添加到队列中。这个节点将是树的根。移动到 arr[] 中的第二个元素,然后执行以下步骤:
- 从temp的队列中弹出NodeDetails 。
- 借助min和temp.data值检查当前数组元素是否可以是temp中节点的左子节点。如果可以,则为这个新的数组元素值创建一个新的NodeDetails结构,并使用其适当的 'min' 和 'max' 值并将其推送到队列中,然后移动到 arr[] 中的下一个元素。
- 借助max和temp.data值检查当前数组元素是否可以是temp中节点的右子节点。如果可以,则为这个新的数组元素值创建一个新的NodeDetails结构,并使用其适当的 'min' 和 'max' 值并将其推送到队列中,然后移动到 arr[] 中的下一个元素。
- 重复步骤 1、2 和 3,直到 arr[] 中没有更多元素或队列中没有更多元素。
最后,如果已经遍历了数组的所有元素,则该数组表示 BST 的级别顺序遍历,否则不是。
C++
// C++ implementation to check if the given array
// can represent Level Order Traversal of Binary
// Search Tree
#include
using namespace std;
// to store details of a node like
// node's data, 'min' and 'max' to obtain the
// range of values where node's left and
// right child's should lie
struct NodeDetails
{
int data;
int min, max;
};
// function to check if the given array
// can represent Level Order Traversal
// of Binary Search Tree
bool levelOrderIsOfBST(int arr[], int n)
{
// if tree is empty
if (n == 0)
return true;
// queue to store NodeDetails
queue q;
// index variable to access array elements
int i=0;
// node details for the
// root of the BST
NodeDetails newNode;
newNode.data = arr[i++];
newNode.min = INT_MIN;
newNode.max = INT_MAX;
q.push(newNode);
// until there are no more elements
// in arr[] or queue is not empty
while (i != n && !q.empty())
{
// extracting NodeDetails of a
// node from the queue
NodeDetails temp = q.front();
q.pop();
// check whether there are more elements
// in the arr[] and arr[i] can be left child
// of 'temp.data' or not
if (i < n && (arr[i] < temp.data &&
arr[i] > temp.min))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.min;
newNode.max = temp.data;
q.push(newNode);
}
// check whether there are more elements
// in the arr[] and arr[i] can be right child
// of 'temp.data' or not
if (i < n && (arr[i] > temp.data &&
arr[i] < temp.max))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.data;
newNode.max = temp.max;
q.push(newNode);
}
}
// given array represents level
// order traversal of BST
if (i == n)
return true;
// given array do not represent
// level order traversal of BST
return false;
}
// Driver program to test above
int main()
{
int arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10};
int n = sizeof(arr) / sizeof(arr[0]);
if (levelOrderIsOfBST(arr, n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation to check if the given array
// can represent Level Order Traversal of Binary
// Search Tree
import java.util.*;
class Solution
{
// to store details of a node like
// node's data, 'min' and 'max' to obtain the
// range of values where node's left and
// right child's should lie
static class NodeDetails
{
int data;
int min, max;
};
// function to check if the given array
// can represent Level Order Traversal
// of Binary Search Tree
static boolean levelOrderIsOfBST(int arr[], int n)
{
// if tree is empty
if (n == 0)
return true;
// queue to store NodeDetails
Queue q = new LinkedList();
// index variable to access array elements
int i = 0;
// node details for the
// root of the BST
NodeDetails newNode=new NodeDetails();
newNode.data = arr[i++];
newNode.min = Integer.MIN_VALUE;
newNode.max = Integer.MAX_VALUE;
q.add(newNode);
// until there are no more elements
// in arr[] or queue is not empty
while (i != n && q.size() > 0)
{
// extracting NodeDetails of a
// node from the queue
NodeDetails temp = q.peek();
q.remove();
newNode = new NodeDetails();
// check whether there are more elements
// in the arr[] and arr[i] can be left child
// of 'temp.data' or not
if (i < n && (arr[i] < (int)temp.data &&
arr[i] > (int)temp.min))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.min;
newNode.max = temp.data;
q.add(newNode);
}
newNode=new NodeDetails();
// check whether there are more elements
// in the arr[] and arr[i] can be right child
// of 'temp.data' or not
if (i < n && (arr[i] > (int)temp.data &&
arr[i] < (int)temp.max))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.data;
newNode.max = temp.max;
q.add(newNode);
}
}
// given array represents level
// order traversal of BST
if (i == n)
return true;
// given array do not represent
// level order traversal of BST
return false;
}
// Driver code
public static void main(String args[])
{
int arr[] = {7, 4, 12, 3, 6, 8, 1, 5, 10};
int n = arr.length;
if (levelOrderIsOfBST(arr, n))
System.out.print( "Yes");
else
System.out.print( "No");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation to check if the
# given array can represent Level Order
# Traversal of Binary Search Tree
INT_MIN, INT_MAX = float('-inf'), float('inf')
# To store details of a node like node's
# data, 'min' and 'max' to obtain the
# range of values where node's left
# and right child's should lie
class NodeDetails:
def __init__(self, data, min, max):
self.data = data
self.min = min
self.max = max
# function to check if the given array
# can represent Level Order Traversal
# of Binary Search Tree
def levelOrderIsOfBST(arr, n):
# if tree is empty
if n == 0:
return True
# queue to store NodeDetails
q = []
# index variable to access array elements
i = 0
# node details for the root of the BST
newNode = NodeDetails(arr[i], INT_MIN, INT_MAX)
i += 1
q.append(newNode)
# until there are no more elements
# in arr[] or queue is not empty
while i != n and len(q) != 0:
# extracting NodeDetails of a
# node from the queue
temp = q.pop(0)
# check whether there are more elements
# in the arr[] and arr[i] can be left
# child of 'temp.data' or not
if i < n and (arr[i] < temp.data and
arr[i] > temp.min):
# Create NodeDetails for newNode
#/ and add it to the queue
newNode = NodeDetails(arr[i], temp.min, temp.data)
i += 1
q.append(newNode)
# check whether there are more elements
# in the arr[] and arr[i] can be right
# child of 'temp.data' or not
if i < n and (arr[i] > temp.data and
arr[i] < temp.max):
# Create NodeDetails for newNode
#/ and add it to the queue
newNode = NodeDetails(arr[i], temp.data, temp.max)
i += 1
q.append(newNode)
# given array represents level
# order traversal of BST
if i == n:
return True
# given array do not represent
# level order traversal of BST
return False
# Driver code
if __name__ == "__main__":
arr = [7, 4, 12, 3, 6, 8, 1, 5, 10]
n = len(arr)
if levelOrderIsOfBST(arr, n):
print("Yes")
else:
print("No")
# This code is contributed by Rituraj Jain
C#
// C# implementation to check if the given array
// can represent Level Order Traversal of Binary
// Search Tree
using System;
using System.Collections.Generic;
class GFG
{
// to store details of a node like
// node's data, 'min' and 'max' to obtain the
// range of values where node's left and
// right child's should lie
public class NodeDetails
{
public int data;
public int min, max;
};
// function to check if the given array
// can represent Level Order Traversal
// of Binary Search Tree
static Boolean levelOrderIsOfBST(int []arr, int n)
{
// if tree is empty
if (n == 0)
return true;
// queue to store NodeDetails
Queue q = new Queue();
// index variable to access array elements
int i = 0;
// node details for the
// root of the BST
NodeDetails newNode=new NodeDetails();
newNode.data = arr[i++];
newNode.min = int.MinValue;
newNode.max = int.MaxValue;
q.Enqueue(newNode);
// until there are no more elements
// in arr[] or queue is not empty
while (i != n && q.Count > 0)
{
// extracting NodeDetails of a
// node from the queue
NodeDetails temp = q.Peek();
q.Dequeue();
newNode = new NodeDetails();
// check whether there are more elements
// in the arr[] and arr[i] can be left child
// of 'temp.data' or not
if (i < n && (arr[i] < (int)temp.data &&
arr[i] > (int)temp.min))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.min;
newNode.max = temp.data;
q.Enqueue(newNode);
}
newNode=new NodeDetails();
// check whether there are more elements
// in the arr[] and arr[i] can be right child
// of 'temp.data' or not
if (i < n && (arr[i] > (int)temp.data &&
arr[i] < (int)temp.max))
{
// Create NodeDetails for newNode
/// and add it to the queue
newNode.data = arr[i++];
newNode.min = temp.data;
newNode.max = temp.max;
q.Enqueue(newNode);
}
}
// given array represents level
// order traversal of BST
if (i == n)
return true;
// given array do not represent
// level order traversal of BST
return false;
}
// Driver code
public static void Main(String []args)
{
int []arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
int n = arr.Length;
if (levelOrderIsOfBST(arr, n))
Console.Write( "Yes");
else
Console.Write( "No");
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
Yes
时间复杂度: O(n)