-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (32 loc) · 828 Bytes
/
Solution.java
File metadata and controls
35 lines (32 loc) · 828 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
34
35
package $098;
import datastruc.TreeNode;
import java.util.LinkedList;
import java.util.List;
/**
* @author Junlan Shuai[shuaijunlan@gmail.com].
* @date Created on 3:07 PM 2018/07/17.
*/
public class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null){
return false;
}
LinkedList<TreeNode> nodes = new LinkedList<>();
pre(root, nodes);
TreeNode temp = nodes.poll();
for (TreeNode node : nodes){
if (node.val <= temp.val){
return false;
}
temp = node;
}
return true;
}
public void pre(TreeNode root, List<TreeNode> list){
if (root != null){
pre(root.left, list);
list.add(root);
pre(root.right, list);
}
}
}