-
Notifications
You must be signed in to change notification settings - Fork 21
/
largest_internal_binary_search_tree.rb
65 lines (53 loc) · 1.8 KB
/
largest_internal_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
require_relative '../data-structures/tree_node'
# Given a binary tree, return its largest internal binary search tree.
# @param {TreeNode} root
# @return {TreeNode.val[]}
def largest_internal_binary_search_tree(root)
binary_search_trees = []
nodes_to_visit = [root]
while nodes_to_visit.count > 0
current_root = nodes_to_visit.pop
current_tree, additional_nodes_to_visit = current_binary_search_tree(current_root)
binary_search_trees << current_tree unless current_tree.count == 1
nodes_to_visit += additional_nodes_to_visit
end
binary_search_trees.max_by(&:count)
end
# @param {TreeNode} root
# @return {[TreeNode[], TreeNode[]]}
def current_binary_search_tree(root)
current_tree = []
unvisited_nodes = [] # Nodes to call current_binary_search_tree on from largest_internal_binary_search_tree
valid_children = []
loop do
if valid_node? root
current_tree << root
if valid_node? root.left
valid_children << root.left
elsif !root.left.nil?
unvisited_nodes << root.left
end
if valid_node? root.right
valid_children << root.right
elsif !root.right.nil?
unvisited_nodes << root.right
end
break if valid_children.count == 0
root = valid_children.shift
else
unvisited_nodes << root.left unless root.left.nil?
unvisited_nodes << root.right unless root.right.nil?
break
end
end
[current_tree, unvisited_nodes]
end
def valid_node?(node)
# The binary search tree property states that the key in each node must be greater than all
# keys stored in the left sub-tree, and smaller than all keys in right sub-tree.
if !node || (node.left && node.data <= node.left.data) || (node.right && node.data >= node.right.data)
false
else
true
end
end