zoukankan      html  css  js  c++  java
  • 力扣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();
        }
    }
  • 相关阅读:
    UVa 10118 记忆化搜索 Free Candies
    CodeForces 568B DP Symmetric and Transitive
    UVa 11695 树的直径 Flight Planning
    UVa 10934 DP Dropping water balloons
    CodeForces 543D 树形DP Road Improvement
    CodeForces 570E DP Pig and Palindromes
    HDU 5396 区间DP 数学 Expression
    HDU 5402 模拟 构造 Travelling Salesman Problem
    HDU 5399 数学 Too Simple
    CodeForces 567F DP Mausoleum
  • 原文地址:https://www.cnblogs.com/zhaojiayu/p/15564262.html
Copyright © 2011-2022 走看看