leetcode98:验证二叉搜索树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isValidBST(TreeNode root) {
        ArrayList list=new ArrayList();
        inOrder(root,list);
        
        for(int i=1;i=list.get(i)){
                return false;
            }
        }
        return true;
    }
    
    private void inOrder(TreeNode node,List list){
        if(node==null) return;
        inOrder(node.left,list);
        list.add(node.val);
        inOrder(node.right,list);
    } 
}
class Solution {
    public boolean isValidBST(TreeNode root) {
       
        BSTIterator it = new BSTIterator(root);
        ArrayList list=new ArrayList();
        while(it.hasNext()){
            list.add(it.next());
        }
        for(int i=0;i=list.get(i+1)){
                return false;
            }
        }
        return true;
 	}
} 	

class BSTIterator {
    LinkedList stack=new LinkedList<>();
    public BSTIterator(TreeNode root) {
        TreeNode cur=root;
        while(cur!=null){
            stack.push(cur);
            cur=cur.left;
        }
    }
    
    /** @return the next smallest number */
    public int next() {
        TreeNode n=stack.pop();
        TreeNode cur=n.right;
        while(cur!=null){
            stack.push(cur);
            cur=cur.left;
        }
        return n.val;
    }
    
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }
}

你可能感兴趣的:(Leetcode刷题)