<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)501:二叉搜索樹中的眾數(shù)

          共 2185字,需瀏覽 5分鐘

           ·

          2022-01-23 13:32

          算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?二叉搜索樹中的眾數(shù),我們先來(lái)看題面:
          https://leetcode-cn.com/problems/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.

          給定一個(gè)有相同值的二叉搜索樹(BST),找出 BST 中的所有眾數(shù)(出現(xiàn)頻率最高的元素)。
          假定 BST 有如下定義:
          結(jié)點(diǎn)左子樹中所含結(jié)點(diǎn)的值小于等于當(dāng)前結(jié)點(diǎn)的值
          結(jié)點(diǎn)右子樹中所含結(jié)點(diǎn)的值大于等于當(dāng)前結(jié)點(diǎn)的值
          左子樹和右子樹都是二叉搜索樹

          示例? ? ? ? ? ? ? ? ? ? ? ? ?


          解題

          https://blog.csdn.net/renweiyi1487/article/details/109489686

          由于二叉樹是有序的因此可以使用二叉樹的中序遍歷,使用一個(gè)pre指針保存當(dāng)前節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn),利用變量count進(jìn)行計(jì)數(shù),計(jì)數(shù)值如果等于最大的頻率則將當(dāng)前元素添加入列表當(dāng)中,如果當(dāng)前計(jì)數(shù)值大于最大的頻率值則更新最大頻率值,并且需要將列表清空,因?yàn)榇藭r(shí)列表中的元素已經(jīng)不再是眾數(shù)了。

          class?Solution?{

          ????// 記錄前一個(gè)指針
          ????private?TreeNode pre = null;

          ????// 計(jì)算出現(xiàn)次數(shù)
          ????private?int?count = 0;

          ????// 最大的出現(xiàn)頻率
          ????private?int?maxCount = 0;

          ????private?List list = new?ArrayList<>();

          ????public?int[] findMode(TreeNode root) {
          ????????searchBST(root);
          ????????int[] ans = new?int[list.size()];
          ????????for?(int?i = 0; i < ans.length; i++) {
          ????????????ans[i] = list.get(i);
          ????????}
          ????????return?ans;
          ????}

          ????private?void?searchBST(TreeNode cur) {
          ????????if?(cur == null) return;
          ????????searchBST(cur.left);
          ????????if?(pre == null) {
          ????????????count = 1;
          ????????} else?if?(pre.val == cur.val) {
          ????????????count++;
          ????????} else?if?(pre.val != cur.val) {
          ????????????count = 1;
          ????????}
          ????????pre = cur;
          ????????if?(count == maxCount) {
          ????????????list.add(cur.val);
          ????????}

          ????????if?(count > maxCount) {
          ????????????maxCount = count;// 更新最大的頻率
          ????????????list.clear(); // 清空列表,之前的元素失效
          ????????????list.add(cur.val);
          ????????}
          ????????searchBST(cur.right);
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:
          LeetCode1-500題匯總,希望對(duì)你有點(diǎn)幫助!


          瀏覽 39
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91操操干| 狠狠狠狠狠插狠狠狠插狠狠狠插 | 爱综合五月天 | 一二三区久在线视频 | 亚洲另类色区欧美日韩 |