首页 > 其他分享 >leetcode-501-easy

leetcode-501-easy

时间:2023-01-22 20:11:27浏览次数:41  
标签:return modeMap findModeRec maxValues easy maxCount root leetcode 501

Find Mode in Binary Search Tree

Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

Assume a BST is defined as follows:

The left subtree of a node contains only nodes with keys less than or equal to the node's key.
The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:


Input: root = [1,null,2,2]
Output: [2]
Example 2:

Input: root = [0]
Output: [0]
Constraints:

The number of nodes in the tree is in the range [1, 104].
-105 <= Node.val <= 105
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

思路一:递归遍历二叉树,用 map 存储节点值

    public int[] findMode(TreeNode root) {
        modeMap.clear();
        findModeRec(root);

        int maxCount = 0;
        List<Integer> maxValues = new ArrayList<>();
        for (Map.Entry<Integer, Integer> e : modeMap.entrySet()) {
            if (e.getValue() > maxCount) {
                maxCount = e.getValue();
                maxValues.clear();
                maxValues.add(e.getKey());
            } else if (e.getValue() == maxCount) {
                maxValues.add(e.getKey());
            }
        }

        return maxValues.stream().mapToInt(Integer::intValue).toArray();
    }

    private static final Map<Integer, Integer> modeMap = new HashMap<>();
    public void findModeRec(TreeNode root) {
        if (root == null) {
            return;
        }

        modeMap.compute(root.val, (k, v) -> v == null ? 1 : v + 1);
        findModeRec(root.left);
        findModeRec(root.right);
    }

思路二:看了官方的题解,可以使用中序遍历,中序遍历相当于遍历有序数组

标签:return,modeMap,findModeRec,maxValues,easy,maxCount,root,leetcode,501
From: https://www.cnblogs.com/iyiluo/p/17064610.html

相关文章

  • leetocde-374-easy
    GuessNumberHigherorLowerWeareplayingtheGuessGame.Thegameisasfollows:Ipickanumberfrom1ton.YouhavetoguesswhichnumberIpicked.Eve......
  • LeetCode最长回文子串(/dp)
    原题解题目约束解法解法一#include<iostream>#include<string>#include<vector>usingnamespacestd;classSolution{public:stringlongestPa......
  • LeetCode_单周赛_329
    2544.交替数字和代码1转成字符串,逐个判断classSolution{publicintalternateDigitSum(intn){char[]s=(""+n).toCharArray();intt......
  • 【队列】LeetCode 281. 锯齿迭代器
    题目链接281.锯齿迭代器思路使用队列进行保存代码publicclassZigzagIterator{Queue<Iterator<Integer>>queue=newLinkedList<>();publicZigzagIt......
  • 【队列】LeetCode 346. 数据流中的移动平均值
    题目链接346.数据流中的移动平均值思路队列设计基础题代码classMovingAverage{privateIntegercurrentSum=0;privateQueue<Integer>queue=newLi......
  • [LeetCode] 684. Redundant Connection
    Inthisproblem,atreeisan undirectedgraph thatisconnectedandhasnocycles.Youaregivenagraphthatstartedasatreewith n nodeslabeledfrom......
  • leetcode笔记——328周赛
    1.二维前缀和,二维差分304.二维区域和检索-矩阵不可变-力扣(LeetCode)二维前缀和怎么处理,注意加减的下标 2.2537.统计好子数组的数目-力扣(LeetCode)首先看到子数......
  • 【双指针】LeetCode 409. 最长回文串
    题目链接409.最长回文串思路遍历字符串过程中统计字符出现个数,如果达到2则说明可以放到回文串的两端,需要result+=2。遍历完之后的回文串如果长度小于s,说明s中存......
  • LeetCode.541 反转字符串II
    1.题目给定一个字符串s和一个整数k,从字符串开头算起,每计数至2k个字符,就反转这2k字符中的前k个字符。如果剩余字符少于k个,则将剩余字符全部反转。如果剩余字符小......
  • LeetCode.面试题02.05-链表求和-题解分析
    题目来源面试题02.05.链表求和题目详情给定两个用链表表示的整数,每个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并......