501-二叉搜索树中的众数

501-二叉搜索树中的众数

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:

  • 结点左子树中所含结点的值小于等于当前结点的值
  • 结点右子树中所含结点的值大于等于当前结点的值
  • 左子树和右子树都是二叉搜索树

例如:
给定 BST [1,null,2,2],

   1
         2
    /
   2

返回[2].

提示:如果众数超过1个,不需考虑输出顺序

进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-mode-in-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    public int[] findMode(TreeNode root) {
        // 1.遍历一次,记录所有元素,就可以找到众数
        List<Integer> list = new ArrayList<>();
        list = scan(list, root);

        Integer[] li = list.toArray(new Integer[0]);

        HashMap<Integer, Integer> map = new HashMap<>();
        int max = 0;
        for(Integer i : li) {
            int tmp = map.getOrDefault(i, 0);
            map.put(i, tmp + 1);
            max = Math.max(max, tmp + 1);
        }
        Set<Integer> set = new HashSet<>();
        for(Integer i : map.keySet()) {
            if(map.get(i).equals(max)) {
                set.add(i);
            }
        }

        Integer[] res = set.toArray(new Integer[0]);
        int[] result = new int[res.length];
        for(int i = 0; i < res.length; i++) {
            result[i] = res[i];
        }
        return result;
    }

    private List<Integer> scan(List<Integer> list, TreeNode root) {
        if (root != null) {
            list = scan(list, root.left);
            list = scan(list, root.right);
            list.add(root.val);
        }
        return list;
    }
    int maxTimes = 0;
    int thisTimes = 0;
    List<Integer> res = new LinkedList<>();
    TreeNode pre = null;
    public int[] findMode(TreeNode root) {
        scan(root);
        int[] rr = new int[res.size()];
        for(int i = 0; i < rr.length; i++) {
            rr[i] = res.get(i);
        }
        return rr;
    }

    public void scan(TreeNode root) {
        if(root == null) {
            return;
        }

        scan(root.left);

        if(pre != null && pre.val == root.val) {
            thisTimes++;
        } else {
            thisTimes = 1;
        }
        if(thisTimes == maxTimes) {
            res.add(root.val);
        } else if(thisTimes > maxTimes) {
            maxTimes = thisTimes;
            res.clear();
            res.add(root.val);
        }
        pre = root;

        scan(root.right);
    }

原文地址:https://www.cnblogs.com/angelica-duhurica/p/12229923.html

时间: 2024-08-30 11:09:19

501-二叉搜索树中的众数的相关文章

[Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

【剑指Offer-树】二叉搜索树中的众数

题目描述 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当前结点的值 左子树和右子树都是二叉搜索树 示例: 例如: 给定 BST [1,null,2,2], 1 2 / 2 返回[2]. 题目链接: https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/ 思路 二叉搜索树

二叉搜索树中的常用方法

1 package Tree; 2 3 import org.junit.Test; 4 5 class TreeNode { 6 7 int val = 0; 8 TreeNode left = null; 9 TreeNode right = null; 10 11 public TreeNode(int val) { 12 this.val = val; 13 14 } 15 16 } 17 18 public class BinarySearchTree { 19 20 /** 21 *

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

leetcode 二叉搜索树中第K小的元素 python

二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且

Leetcode 701. 二叉搜索树中的插入操作

题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可. 你可以返回任意有效的结果. 例如, 给定二叉搜索树: 4 / 2 7 / 1 3 和 插入的值: 5 你可以返回这个

[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

230. 二叉搜索树中第K小的元素

230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 解题思路 中序遍历,利用Python3中提供的生成器方法: 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素: 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找: 实现 class Solution:    

算法dfs——二叉搜索树中最接近的值 II

901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.000000 1 输出: [1] 解释: 二叉树 {1},表示如下的树结构: 1 样例 2: 输入: {3,1,4,#,2} 0.275000 2 输出: [1,2] 解释: 二叉树 {3,1,4,#,2},表示如下的树结构: 3 / 1 4 2 挑战 假设是一棵平衡二叉搜索树,你可以用时间复杂度低于O(n)