1. 程式人生 > >LeetCode-501. Find Mode in Binary Search Tree(Java)

LeetCode-501. Find Mode in Binary Search Tree(Java)

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 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.

For example:
Given BST [1,null,2,2],

   1
    \
     2
    /
   2

return [2].

------------------------------------------------------------------------------------------------------------------------------------

題意

從一個二叉搜尋樹中找到出現最頻繁的元素,即出現次數最多的元素。

思路

採用三個遍歷方式之一;因為要記錄相同元素出現的次數,所以需要記錄上一個節點用來判斷,如果上一個節點與當前節點相同,

則出現次數加1,否則,出現次數重置,當前節點賦值給上一個節點;同時記錄大於等於出現最大次數的元素,所以需要一個數組,

然後需要一個計數器,當如果上一個節點與當前節點不相同時並且當前節點出現次數大於或等於最大出現次數,把該節點值新增到陣列,

並且該計數器加1。大致思路如此

程式碼

public class Solution {
    public int[] findMode(TreeNode root) {
        inorder(root);
        modes = new int[modeCount];
        modeCount = 0;
        currCount = 0;
        inorder(root);
        return modes;
    }
    //記錄元素值
    private int currVal;
    //記錄相同元素出現次數
    private int currCount = 0;
    //記錄出現的最大次數
    private int maxCount = 0;
    //作為modes陣列的下標,記錄出現相同最大次數的元素個數
    private int modeCount = 0;
    
    private int[] modes;
    private void handleValue(int val) {
        if (val != currVal) {
            currVal = val;
            currCount = 0;
        }
        currCount++;
        if (currCount > maxCount) {
            maxCount = currCount;
            modeCount = 1;
        } else if (currCount == maxCount) {
            if (modes != null)
                modes[modeCount] = currVal;
            modeCount++;
        }
    }
    
    private void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        handleValue(root.val);
        inorder(root.right);
    }
}

相關推薦

LeetCode-501. Find Mode in Binary Search Tree(Java)

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assum

LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索樹的眾數)

btn https 標簽 one con pac 發現 log 個數字 Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred

[LeetCode]501. Find Mode in Binary Search Tree二叉搜索樹尋找眾數

pri fin lis 需要 efi lee value spa find 這次是二叉搜索樹的遍歷 感覺只要和二叉搜索樹的題目,都要用到一個重要性質: 中序遍歷二叉搜索樹的結果是一個遞增序列; 而且要註意,在遞歸遍歷樹的時候,有些參數如果是要隨遞歸不斷更新(也就是如果遞歸返

[LeetCode] 501. Find Mode in Binary Search Tree

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

LeetCode-501. Find Mode in Binary Search Tree

問題:https://leetcode.com/problems/find-mode-in-binary-search-tree/?tab=Description Given a binary search tree (BST) with duplicates

Leetcode 501 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. Assum

[Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree

upd bsp have values greate pop mes strong print Given a binary search tree (BST) with duplicates, find all the mode(s) (the most freque

[leetcode: Python]501. 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. As

501. Find Mode in Binary Search Tree

element arraylist ati temp 哪些 arc org href search Given a binary search tree (BST) with duplicates, find all the mode(s) (the most freque

501. Find Mode in Binary Search Tree查找BST中的眾數

pan 為什麽 AR dup 不用 lan 結果 with bug [抄題]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occur

501. Find Mode in Binary Search Tree - Easy

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

501. Find Mode in Binary Search Tree#2

Solution#1 Problem#1 1. 沒什麼想法 2. int不能為null,但是Integer可以 3. Integer初始化是考慮是否置null Problem#2 1.

[LeetCode] 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

leetcode[Find Mode in Binary Search Tree]//待整理多種解法

解法一: /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tre

Find Mode in Binary Search Tree 二叉搜索樹中查找眾數

class div 問題 xtra 空間分析 deb spa pos out [抄題]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently

132.Find Mode in Binary Search Tree(二分搜索樹的眾數)

ati 之前 復雜度 i++ cit 二分搜索 無法 treenode 重復 題目: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc

Leetcode501.Find Mode in Binary Search Tree二叉搜尋樹中的眾數

給定一個有相同值的二叉搜尋樹(BST),找出 BST 中的所有眾數(出現頻率最高的元素)。 假定 BST 有如下定義: 結點左子樹中所含結點的值小於等於當前結點的值 結點右子樹中所含結點的值大於等於當前結點的值 左子樹和右子樹都是二叉搜尋樹 例如: 給定 B

[LeetCode] Verify Preorder Sequence in Binary Search Tree 驗證二叉搜尋樹的先序序列

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the seque

find second maximum element in Binary search tree

roo arc 一個 gpo sharp div find serial val 一個BST, 問怎麽找到第二大的節點。首先說了個naive的方法,serialize, 他問有沒有更有效的方法。最後用的iterative的方法向右遍歷子節點 log(n) 或者inorder

[leetcode 255] Verify Preorder Sequence in Binary Search Tree ---先序遍歷驗證二叉搜尋樹

Question: Given an array of numbers, verify whether it is the correct preorder traversal sequence of