# 檢查給定的數組是否可以表示二進制搜索樹的級別順序遍歷

## 例

`Level Order Traversal - {5 2 7 1 6 9 }`
`True`

## 推薦碼

### 用於檢查給定數組是否可以表示二進制搜索樹的級別順序遍歷的C ++代碼

```#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
int mn;
int mx;
};

node create(int data, int mn, int mx){
node tmp;
tmp.data = data;
tmp.mn = mn;
tmp.mx = mx;
return tmp;
}

bool checkLevelOrderTraversalRepresentBinarySearchTree(vector<int> traversal){
queue<node> q;
int i = 0, n = traversal.size();
q.push(create(traversal[i++], INT_MIN, INT_MAX));

while(!q.empty()){
node now = q.front();
q.pop();
if(i<n && now.mn<traversal[i] && traversal[i]<now.data)
q.push(create(traversal[i++], now.mn, now.data));
if(i<n && now.data<traversal[i] && traversal[i]<now.mx)
q.push(create(traversal[i++], now.data, now.mx));
}
return (i == n);
}

int main()
{
int t;cin>>t;
while(t--){
int n;cin>>n;
vector<int> traversal(n);
for(int i=0;i<n;i++)
cin>>traversal[i];
cout<<(checkLevelOrderTraversalRepresentBinarySearchTree(traversal) ? "true" : "no")<<endl;
}
}
```
```1
6
5 2 7 1 6 9```
`true`

### 用於檢查給定數組是否可以表示二進制搜索樹的級別順序遍歷的Java代碼

```import java.util.*;
import java.lang.*;
import java.io.*;

class node{
int data;
int mn;
int mx;
}

class Main{

static node create(int data, int mn, int mx){
node tmp = new node();
tmp.data = data;
tmp.mn = mn;
tmp.mx = mx;
return tmp;
}

static boolean checkLevelOrderTraversalRepresentBinarySearchTree(int traversal[]){
int i = 0; int n = traversal.length;

while(q.size() > 0){
node now = q.peek();
q.remove();
if(i<n && now.mn<traversal[i] && traversal[i]<now.data)
if(i<n && now.data<traversal[i] && traversal[i]<now.mx)
}
return (i == n);
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int[] traversal = new int[n];
for(int i=0;i<n;i++)
traversal[i] = sc.nextInt();
System.out.println(checkLevelOrderTraversalRepresentBinarySearchTree(traversal) ? "true" : "no");
}
}
}```
```1
6
5 2 7 1 6 9```
`true`