力扣530题、501题(二叉搜索树的最小绝对差,众数)

530、二叉搜索树的最小绝对差

基本思想:

中序遍历

具体实现:

需要记录前一节点,用pre记

 

代码:

class Solution {
    TreeNode pre;//记录上一个遍历的节点
    int result = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) return 0 ;
        traversal(root);
        return result;
    }

    public void traversal(TreeNode root){
        if (root == null) return;
        traversal(root.left);
        if (pre != null) result = Math.min(result, root.val - pre.val);
        pre = root;
        traversal(root.right);
    }
}

 

 

迭代法:

class Solution {
    TreeNode pre;
    Stack<TreeNode> stack;
    public int getMinimumDifference(TreeNode root) {
        if (root == null) return 0;
        stack = new Stack<>();
        TreeNode cur = root;
        int result = Integer.MAX_VALUE;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur); // 将访问的节点放进栈
                cur = cur.left; //
            }else {
                cur = stack.pop(); 
                if (pre != null) { //
                    result = Math.min(result, cur.val - pre.val);
                }
                pre = cur;
                cur = cur.right; //
            }
        }
        return result;
    }
}

 

 

501、二叉搜索树中的众数

基本思想:

中序遍历

具体实现:

需要记录前一节点,用pre记

弄一个指针指向前一个节点,这样每次cur(当前节点)才能和pre(前一个节点)作比较。

而且初始化的时候pre = NULL,这样当pre为NULL时候,我们就知道这是比较的第一个元素。

如果 频率count 等于 maxCount(最大频率),当然要把这个元素加入到结果集中(以下代码为result数组),

频率count 大于 maxCount的时候,不仅要更新maxCount,而且要清空结果集(以下代码为result数组),因为结果集之前的元素都失效了。

代码:

class Solution {
    ArrayList<Integer> resList;
    int maxCount;
    int count;
    TreeNode pre;
    public int[] findMode(TreeNode root) {
        resList = new ArrayList<>();
        maxCount = 0;
        count = 0;
        pre = null;
        findMode1(root);
        int[] res = new int[resList.size()];
        for (int i = 0; i < resList.size(); i++){
            res[i] = resList.get(i);
        }
        return res;
    }

    public void findMode1(TreeNode root){
        if (root == null) return;
        findMode1(root.left);
        
        int rootValue = root.val;
        if (pre == null || rootValue != pre.val){
            count = 1;
        } else {
            count++;
        }

        if (count > maxCount) {
            resList.clear();
            resList.add(rootValue);
            maxCount = count;
        } else if (count == maxCount) {
            resList.add(rootValue);
        }

        pre = root;

        findMode1(root.right);
    }
}

 

迭代法

class Solution {
    public int[] findMode(TreeNode root) {
        TreeNode pre = null;
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        int maxCount = 0;
        int count = 0;
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();

                //执行操作
                if (pre == null || cur.val != pre.val){
                    count = 1;
                } else {
                    count++;
                }
                if (count > maxCount){
                    maxCount = count;
                    result.clear();
                    result.add(cur.val);
                } else if (count == maxCount){
                 result.add(cur.val);
                }
                pre = cur;
                //操作完毕


                
                cur = cur.right;
            }
        }
        return result.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

posted @ 2021-11-16 22:49  最近饭吃的很多  阅读(30)  评论(0编辑  收藏  举报