public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; }
}
class Solution {
public void reverse(TreeNode root, List<Integer> values) {
if (root == null) return;
traverse(root.left, values);
values.add(root.val);
traverse(root.right, values);
}
public boolean isValidBST(TreeNode root) {
List<Integer> values = new LinkedList<>();
traverse(root, values);
for (int i = 0; i < values.size() - 1; i++) {
if (values.get(i) >= values.get(i + 1)) return false;
}
return true;
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
private long pre = Long.MIN_VALUE;
public boolean traverse(TreeNode root) {
if (root == null) return true;
boolean ans = traverse(root.left) && this.pre < root.val;
if (ans == false) return false;
this.pre = root.val;
return traverse(root.right);
}
public boolean isValidBST(TreeNode root) {
return traverse(root);
}
}