1. 程式人生 > >132.Find Mode in Binary Search Tree(二分搜索樹的眾數)

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 occurred element) in the given BST.

給定具有重復項的二叉搜索樹(BST),找到給定BST中的所有眾數(最頻繁出現的元素)。

Assume a BST is defined as follows:

假設BST定義如下:

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

Note: If a tree has more than one mode, you can return them in any order.

註意:如果樹有多個模式,您可以按任何順序返回它們。

Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

跟進:你可以不使用任何額外的空間嗎? (假設由於遞歸而產生的隱式堆棧空間不計算)。

解答:

方法一:時間復雜度:O(n),空間復雜度:O(n)

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 Map<Integer,Integer> map=new HashMap<>(); 12 int maxTimes; 13 public int[] findMode(TreeNode root) { 14 if(root==null) return new int[0]; 15 inorder(root); 16 17 List<Integer> list=new ArrayList<>(); 18 for(int key:map.keySet()){ 19 if(map.get(key)==maxTimes) 20 list.add(key); 21 } 22 23 int[] res=new int[list.size()]; 24 for(int i = 0; i<res.length; i++) 25 res[i] = list.get(i); 26 return res; 27 } 28 29 private void inorder(TreeNode node){ 30 if(node.left!=null) inorder(node.left); 31 map.put(node.val,map.getOrDefault(node.val,0)+1); 32 maxTimes=Math.max(maxTimes,map.get(node.val)); 33 if(node.right!=null) inorder(node.right); 34 } 35 }

方法二:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     Integer prev=null;
12     int maxTimes=0;
13     int count=1;
14     
15     public int[] findMode(TreeNode root) {
16         if(root==null) return new int[0];
17         
18         List<Integer> list=new ArrayList<>();
19         inorder(root,list);
20         
21         int[] res=new int[list.size()];
22         for(int i=0;i<list.size();i++)
23             res[i]=list.get(i);
24         return res;
25     }
26     
27     private void inorder(TreeNode node,List<Integer> list){
28         if(node==null) return;
29         inorder(node.left,list);
30         if(prev!=null){
31             if(node.val==prev)
32                 count++;
33             else
34                 count=1;
35         }
36         if(count>maxTimes){
37             list.clear();
38             list.add(node.val);
39             maxTimes=count;
40         }else if(count==maxTimes){
41             list.add(node.val);
42         }
43         prev=node.val;
44         inorder(node.right,list);
45     }
46 }

詳解:

方法一:

哈希表:記錄數字和其出現次數之前的映射,變量maxTimes:記錄當前最多的次數值(適用於任何尋找眾數的樹,任意一種遍歷方式均可,這裏選擇中序遍歷)

方法二:

題目跟進,不需要額外存儲空間,則無法使用哈希表。利用二分搜索樹的性質,本身即有序,相同的元素連在一起

prev:相同元素的第一個節點

maxTimes:最大次數

count:當前元素出現次數

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