[Algorithm] BST问题

BST性质:左边都比root小,右边都比root大

BST的中序遍历得到的节点访问顺序是从小到大的顺序

98. Valid Binary Search Tree: 点击打开链接

方法一

思路:先中序遍历BST拿到结果list,遍历list里元素,看当前的元素值是不是小于下一个元素值,只要有当前元素值大于等于下一个元素值的,就返回false

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: True if the binary tree is BST, or false
     */
    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        
        List<TreeNode> result=inOrder(root);
        for(int i=0;i<result.size()-1;i++){
            if(result.get(i).val>=result.get(i+1).val){
                return false;
            }
        }
        return true;
    }
    
    private List<TreeNode> inOrder(TreeNode root){
        List<TreeNode> list=new ArrayList<>();
        
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null || !stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            cur=stack.pop();
            list.add(cur);
            cur=cur.right;
        }
        return list;
    }
}
方法二

思路:递归,边遍历边比较

/**
 * 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) {
        return helper(root,Long.MAX_VALUE, Long.MIN_VALUE);
        
    }
    public boolean helper(TreeNode root,long maxVal,long minVal){
        if(root==null){
            return true;
        }
        
        if(root.val>=maxVal || root.val<=minVal){
            return false;
        }
        return helper(root.left,root.val,minVal) && helper(root.right,maxVal,root.val);
    }  
}
11. Search Range in Binary Search Tree:  点击打开链接
思路:中序遍历得到list,再遍历拿到list里每一个符合条件的值
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of the binary search tree.
     * @param k1 and k2: range k1 to k2.
     * @return: Return all keys that k1<=key<=k2 in ascending order.
     */
    public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
        ArrayList<Integer> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        
        ArrayList<Integer> list=inorder(root);
        for(int i=0;i<list.size();i++){
            if(list.get(i)>=k1 && list.get(i)<=k2){
                result.add(list.get(i));
            }
        }
        return result;
    }
    
    private ArrayList<Integer> inorder(TreeNode root){
        ArrayList<Integer> result=new ArrayList<>();
        if(root==null){
            return result;
        }
        Stack<TreeNode> stack=new Stack<>();
        
        TreeNode cur=root;
        while(!stack.isEmpty() || cur!=null){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            cur=stack.pop();
            result.add(cur.val);
            cur=cur.right;
        }
        return result;
    }
}
173.Binary Search Tree Iterator: 点击打开链接

思路:就是BST中序遍历的拆解形式

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    Stack<TreeNode> stack;
    
    public BSTIterator(TreeNode root) {
        stack=new Stack<>();
        AddNodeToStack(root);
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    /** @return the next smallest number */
    public int next() {
        TreeNode cur=stack.pop();
        AddNodeToStack(cur.right);
        return cur.val;
    }
    
     private void AddNodeToStack(TreeNode root){
        while(root!=null){
            stack.push(root);
            root=root.left;
        }
    }
}

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值