【二叉搜索树】【前后指针】Leetcode 501. 二叉搜索树中的众数

在这里插入图片描述

---------------🎈🎈题目链接🎈🎈-------------------

解法1 中序遍历+双指针

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    List<Integer> mylist = new ArrayList<>();
    TreeNode pre = null;
    int count = 1;
    int maxcount = 1;

    public int[] findMode(TreeNode root) {
        // 中序递归+双指针pre cur比较
        helper(root);
        int[] result = new int[mylist.size()];
        for(int i = 0; i< mylist.size(); i++){
            result[i] = mylist.get(i);
        }
        return result;

    }

    public void helper(TreeNode root){
        // 中序遍历
        if(root == null) return;

        helper(root.left);//左


        //  // 计数
       if(pre==null || pre.val!=root.val){  // 初始化count 遇到不相等就置count为1 表示当前1个相同的元素
           count = 1;
       }
        else if(pre!=null && pre.val==root.val){ // 满足前后相等条件count++
            count ++;
        }
        //  // 更新结果集
        if(count > maxcount){
            maxcount = count;
            mylist.clear();
            mylist.add(root.val);
        }
        else if(count == maxcount){
            mylist.add(root.val);
        }

        pre = root;

        helper(root.right); // 右
    }
}        
    

解法2 我的复杂方法 先中序遍历到数组,之后hashmap遍历判断众数 之后转化为数组输出

一些方法汇总:

  1. 遍历hashmap的key-value
 for(var keyvalue:myhash.entrySet()){
 	keyvalue.getKey();
 	keyvalue.getValue();
 }

2.清空arraylist:arr.clear
3.最小值Integer.MIN_VALUE
4.ArrayList转化为int[ ]输出

int[] finalresult = new int[result.size()];
        for(int i = 0; i < result.size();i++){
            finalresult[i] = result.get(i);
        }

代码:::::

class Solution {
    public int[] findMode(TreeNode root) {
       
        List<Integer> mylist = new ArrayList<>();
        helper(root, mylist);
        // 取出众数
        HashMap<Integer,Integer> myhash = new HashMap<>();
        for(int i = 0; i < mylist.size();i++){
            myhash.put(mylist.get(i),myhash.getOrDefault(mylist.get(i),0)+1);
        }
        // 遍历hash
        int max = Integer.MIN_VALUE;
        List<Integer> result = new ArrayList<>();
        for(var keyvalue:myhash.entrySet()){
            if(keyvalue.getValue() > max){
                result.clear();
                result.add(keyvalue.getKey());
                max = keyvalue.getValue();
            }
            else if(keyvalue.getValue() == max){
                result.add(keyvalue.getKey());
            }
        }

        // 输出数组
        int[] finalresult = new int[result.size()];
        for(int i = 0; i < result.size();i++){
            finalresult[i] = result.get(i);
        }
        return finalresult;
    }
    
    public void helper(TreeNode root, List<Integer> mylist){  // 中序遍历的到数组里
        if(root == null) return ;
        helper(root.left,mylist);
        mylist.add(root.val);
        helper(root.right,mylist);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值