-
-
Save unilecs/8f36841b61aa1c202430689b2b59b725 to your computer and use it in GitHub Desktop.
Задача 80: Двоичное дерево поиска
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static bool IsBst(int[] arr) | |
{ | |
// обьявим границы входных чисел | |
int max = int.MaxValue; | |
int min = int.MinValue; | |
int prev = arr[0]; | |
int cur; | |
for (int i = 1; i < arr.Length; i++) | |
{ | |
cur = arr[i]; | |
// Если значение cur не принадлежит интервалу[min; max], | |
// то искомого пути в дереве не существует. | |
if (cur < min || cur > max) | |
{ | |
return false; | |
} | |
// Изменяем границы интервала [min; max] | |
if (cur > prev) | |
{ | |
min = prev; | |
} | |
else | |
{ | |
max = prev; | |
} | |
prev = cur; | |
} | |
return true; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
int[] arr1 = new int[] { 8, 3, 6, 4 }; | |
int[] arr2 = new int[] { 8, 4, 6, 3 }; | |
Console.WriteLine(string.Format("Answer = {0}", IsBst(arr1))); // True | |
Console.WriteLine(string.Format("Answer = {0}", IsBst(arr2))); // False | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment