-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateBinarySearchTree.cs
More file actions
33 lines (28 loc) · 952 Bytes
/
Copy pathValidateBinarySearchTree.cs
File metadata and controls
33 lines (28 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace algorithms.LeetCode;
using algorithms.Extensions;
public class ValidateBinarySearchTree {
public void Test()
{
//[5,4,6,null,null,3,7]
var root2 = new TreeNode(5,
new TreeNode(4),
new TreeNode(6,
new TreeNode(3),
new TreeNode(7)));
IsValidBST(root2).Dump();
array.Dump();
}
public bool IsValidBST(TreeNode root) {
InorderTraversal(root);
var sorted = array.OrderBy(x => x).Distinct().ToList();
if(sorted.Count < this.array.Count) return false;
return this.array.Zip(sorted).All(x => x.First == x.Second);
}
private List<int> array = new (20);
public void InorderTraversal(TreeNode root) {
if(root == null) return;
InorderTraversal(root.left);
array.Add(root.val);
InorderTraversal(root.right);
}
}