forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
33 lines (26 loc) · 805 Bytes
/
Solution.java
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
/// Source : https://leetcode.com/problems/validate-binary-search-tree/description/
/// Author : liuyubobobo
/// Time : 2018-05-22
import java.util.ArrayList;
/// Using inOrder traverse
/// Store all elements in an ArrayList
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public boolean isValidBST(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
inOrder(root, list);
for(int i = 1 ; i < list.size() ; i ++)
if(list.get(i - 1) >= list.get(i))
return false;
return true;
}
private void inOrder(TreeNode node, ArrayList<Integer> list){
if(node == null)
return;
inOrder(node.left, list);
list.add(node.val);
inOrder(node.right, list);
}
}