Skip to content

Commit b6acb32

Browse files
committed
validate binary search tree
1 parent 9baa604 commit b6acb32

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/**
2+
풀이 :
3+
루트에서부터 DFS를 통해 내려가면서, 조상노드에 대한 값을 저장하고 비교하면서 검증하는 방식
4+
5+
복잡도 계산 :
6+
TreeNode의 개수 -> N
7+
시간 복잡도 : O(N^2)
8+
공간 복잡도 : O(N)
9+
*/
10+
11+
class Solution1 {
12+
enum Direction {
13+
LEFT, RIGHT;
14+
}
15+
16+
class Node{
17+
int val;
18+
Direction direction;
19+
Node(int val, Direction d) {
20+
this.val = val;
21+
this.direction = d;
22+
}
23+
}
24+
25+
public boolean isValidBST(TreeNode root) {
26+
27+
return validateTree(root, true, new ArrayList<Node>());
28+
}
29+
30+
public boolean validateTree(TreeNode node, boolean isRoot, List<Node> grandParents) {
31+
if (node == null) {
32+
return true;
33+
}
34+
35+
if (!isRoot) {
36+
if (!isValid(node.val, grandParents)) {
37+
return false;
38+
}
39+
}
40+
41+
Node left = new Node(node.val, Direction.LEFT);
42+
grandParents.add(left);
43+
boolean isLeftValidate = validateTree(node.left, false, grandParents);
44+
grandParents.remove(left);
45+
Node right = new Node(node.val, Direction.RIGHT);
46+
grandParents.add(right);
47+
boolean isRightValidate = validateTree(node.right, false, grandParents);
48+
grandParents.remove(right);
49+
return isLeftValidate && isRightValidate;
50+
}
51+
52+
public boolean isValid(int value, List<Node> grandParents) {
53+
for (Node gpNode : grandParents) {
54+
if (gpNode.direction == Direction.LEFT) {
55+
if (!(value < gpNode.val)) {
56+
return false;
57+
}
58+
} else {
59+
if (!(value > gpNode.val)) {
60+
return false;
61+
}
62+
}
63+
}
64+
return true;
65+
}
66+
}
67+
68+
/**
69+
풀이 :
70+
루트에서부터 DFS를 통해 내려가면서, 조상노드에 대한 값을 저장하고 비교하면서 검증하는 방식
71+
72+
복잡도 계산 :
73+
TreeNode의 개수 -> N
74+
시간 복잡도 : O(NlogN)
75+
공간 복잡도 : O(N)
76+
*/
77+
class Solution2 {
78+
enum Direction {
79+
LEFT, RIGHT;
80+
}
81+
82+
class Parents {
83+
PriorityQueue<Integer> left;
84+
PriorityQueue<Integer> right;
85+
86+
public Parents() {
87+
left = new PriorityQueue<>();
88+
right = new PriorityQueue<>(Collections.reverseOrder());
89+
}
90+
}
91+
92+
public boolean isValidBST(TreeNode root) {
93+
94+
return validateTree(root, true, new Parents());
95+
}
96+
97+
public boolean validateTree(TreeNode node, boolean isRoot, Parents parents) {
98+
if (node == null) {
99+
return true;
100+
}
101+
102+
if (!isRoot) {
103+
if (!isValid(node.val, parents)) {
104+
return false;
105+
}
106+
}
107+
108+
parents.left.add(node.val);
109+
boolean isLeftValidate = validateTree(node.left, false, parents);
110+
parents.left.remove(node.val);
111+
parents.right.add(node.val);
112+
boolean isRightValidate = validateTree(node.right, false, parents);
113+
parents.right.remove(node.val);
114+
115+
return isLeftValidate && isRightValidate;
116+
}
117+
118+
public boolean isValid(int value, Parents parents) {
119+
if(!parents.left.isEmpty()) {
120+
if(!(value < parents.left.peek())) {
121+
return false;
122+
}
123+
}
124+
125+
if(!parents.right.isEmpty()) {
126+
if(!(value > parents.right.peek())) {
127+
return false;
128+
}
129+
}
130+
return true;
131+
}
132+
}
133+
134+
135+
/**
136+
풀이 :
137+
루트에서부터 DFS를 통해 내려가면서, 범위를 비교하면서 검증하는 방식
138+
139+
복잡도 계산 :
140+
TreeNode의 개수 -> N
141+
시간 복잡도 : O(N)
142+
공간 복잡도 : O(N)
143+
*/
144+
class Solution {
145+
146+
public boolean isValidBST(TreeNode root) {
147+
148+
return validateTree(root, true, Long.MIN_VALUE, Long.MAX_VALUE);
149+
}
150+
151+
public boolean validateTree(TreeNode node, boolean isRoot, long min, long max) {
152+
if (node == null) {
153+
return true;
154+
}
155+
156+
if (!isRoot) {
157+
if (!isValid(node, min, max)) {
158+
return false;
159+
}
160+
}
161+
162+
return validateTree(node.left, false, min, node.val) && validateTree(node.right, false, node.val, max);
163+
}
164+
165+
public boolean isValid(TreeNode node, long min, long max) {
166+
167+
if(!(node.val > min)) {
168+
return false;
169+
}
170+
171+
if(!(node.val < max)) {
172+
return false;
173+
}
174+
175+
return true;
176+
}
177+
}
178+

0 commit comments

Comments
 (0)