链接:https://leetcode-cn.com/problems/validate-binary-search-tree/
class Solution {
public boolean isValidBST(TreeNode root) {
BSTIterator it=new BSTIterator(root);
ArrayList<Integer> list=new ArrayList<Integer>();
while(it.hasNext()){
list.add(it.next());
}
for(int i=1;i<list.size();i++){
if(list.get(i-1)>=list.get(i)){
return false;
}
}
return true;
}
class BSTIterator {
LinkedList<TreeNode> stack=new LinkedList<>();
public BSTIterator(TreeNode root) {
TreeNode cur=root;
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
}
public int next() {
TreeNode n=stack.pop();
TreeNode cur=n.right;
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
return n.val;
}
public boolean hasNext() {
return !stack.isEmpty();
}
}
}