-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathSolution.java
34 lines (34 loc) · 880 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
34
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root==null) {
return true;
}
List<Integer> list=inorder(root,new ArrayList<Integer>());
if (list.size()<2) {
return true;
}
for (int i = 0; i < list.size()-1; i++) {
if (list.get(i)>=list.get(i+1)) {
return false;
}
}
return true;
}
public List<Integer> inorder(TreeNode root,List<Integer> list){
if (root!=null) {
list=inorder(root.left,list);
list.add(root.val);
list=inorder(root.right,list);
}
return list;
}
}