-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0501-find-mode-in-binary-search-tree.rb
66 lines (56 loc) · 1.73 KB
/
0501-find-mode-in-binary-search-tree.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
# 501. Find Mode in Binary Search Tree
# Easy
# https://leetcode.com/problems/find-mode-in-binary-search-tree
=begin
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
=end
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @return {Integer[]}
def find_mode(root)
inorder(root, 0, 0, nil, modes = [])
modes
end
def inorder(root, local_max, global_max, prev, modes)
return [local_max, global_max, prev] unless root
local_max, global_max, prev = inorder(root.left, local_max, global_max, prev, modes)
val = root.val
if prev&.val == root.val
local_max += 1
else
local_max = 1
end
if local_max == global_max
modes << val
elsif local_max > global_max
global_max = local_max
modes.clear
modes << val
end
prev = root
local_max, global_max, prev = inorder(root.right, local_max, global_max, prev, modes)
[local_max, global_max, prev]
end