LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索樹的眾數)
阿新 • • 發佈:2017-07-08
btn https 標簽 one con pac 發現 log 個數字
題目標簽:Tree 這道題目給了我們一個二叉搜索樹,讓我們找到樹的眾樹 (出現最多的那個值),可以是一個眾樹,也可以有很多個。看完題目第一個想到用HashMap,但是發現題目最後follow up說不能用extra space。所以我們要另外考慮方法。二叉搜索樹的特性,左邊 <= 根 <= 右邊,這道題目包括了等於。舉一個例子: 10 / \ 5 13 / \ \ 3 7 13 / \ \ 2 3 9 看這個例子,我們試著把它上下壓縮一下, 就等於, 2 3 3 5 7 9 10 13 13 ,在紙上畫的左右分開比較容易看清。發現這是一個有序的排列。如果我們可以遍歷這個順序的話,它是從小到大的,特點就是,一樣的數字一定是連在一起的。這樣我們就可以設一個count = 1,一個maxCount = 0 和一個pre Node, count是記錄每一個數字的連續出現次數,如果大於maxCount 那就說明這個數字是新的mode,比起之前的數字。 maxCount 記錄最大出現次數的mode。pre Node是上一個點的數字,當每次current 點和上一個點比較,是否兩個數字相同,來判斷需要count++,如果不相同,那就更新count = 1。 如何得到這個有序排列,可以通過inorder traversal 來實現,需要註意的是, Java 是 Pass by Value 的核心 ,所以pre node , count 什麽的,需要放在function 外面。
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]
.
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).
題目標簽:Tree 這道題目給了我們一個二叉搜索樹,讓我們找到樹的眾樹 (出現最多的那個值),可以是一個眾樹,也可以有很多個。看完題目第一個想到用HashMap,但是發現題目最後follow up說不能用extra space。所以我們要另外考慮方法。二叉搜索樹的特性,左邊 <= 根 <= 右邊,這道題目包括了等於。舉一個例子: 10 / \ 5 13 / \ \ 3 7 13 / \ \ 2 3 9 看這個例子,我們試著把它上下壓縮一下, 就等於, 2 3 3 5 7 9 10 13 13 ,在紙上畫的左右分開比較容易看清。發現這是一個有序的排列。如果我們可以遍歷這個順序的話,它是從小到大的,特點就是,一樣的數字一定是連在一起的。這樣我們就可以設一個count = 1,一個maxCount = 0 和一個pre Node, count是記錄每一個數字的連續出現次數,如果大於maxCount 那就說明這個數字是新的mode,比起之前的數字。 maxCount 記錄最大出現次數的mode。pre Node是上一個點的數字,當每次current 點和上一個點比較,是否兩個數字相同,來判斷需要count++,如果不相同,那就更新count = 1。 如何得到這個有序排列,可以通過inorder traversal 來實現,需要註意的是, Java 是 Pass by Value 的核心
Java Solution:
Runtime beats 74.31%
完成日期:07/07/2017
關鍵詞:Tree
關鍵點:inorder 遍歷 (從小到大順序)
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 public class Solution 11 { 12 TreeNode pre = null; 13 int cnt = 1; 14 int max_cnt = 0; 15 16 public int[] findMode(TreeNode root) 17 { 18 ArrayList<Integer> res = new ArrayList<>(); 19 20 inorder(root, res); 21 22 int[] result = new int[res.size()]; 23 24 for(int i=0; i<result.length; i++) 25 result[i] = res.get(i); 26 27 return result; 28 } 29 30 public void inorder(TreeNode node, ArrayList<Integer> res) 31 { 32 if(node == null) 33 return; 34 35 inorder(node.left, res); 36 // meaning this node has a previous node, need to compare them to determine cnt 37 if(pre != null) 38 { 39 if(node.val == pre.val) // if this node has same value as pre‘s 40 cnt++; 41 else // if this node has different value as pre‘s 42 cnt = 1; 43 } 44 45 // once cnt is greater max_cnt, meaning find a new mode, need to clear res; 46 if(cnt > max_cnt) 47 { 48 max_cnt = cnt; 49 res.clear(); 50 res.add(node.val); 51 } 52 else if(cnt == max_cnt) // cnt == max_cnt, meaning find a new mode that equal to pre mode. 53 res.add(node.val); 54 55 56 if(pre == null) // for first most left leaf node, its pre is null, set the first pre node 57 { 58 pre = new TreeNode(node.val); 59 } 60 else // if pre is not null, update this node‘s val each time 61 pre.val = node.val; 62 63 64 inorder(node.right, res); 65 } 66 }
參考資料:
http://www.cnblogs.com/grandyang/p/6436150.html
LeetCode 算法題目列表 - LeetCode Algorithms Questions List
LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索樹的眾數)