forked from haozeng/exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlower_common_ancestor.rb
62 lines (52 loc) · 1.22 KB
/
lower_common_ancestor.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
require 'pry'
class Node
attr_accessor :value, :left, :right, :name
def initialize(options={})
@value = options[:value]
@name = options[:name] || nil
@left = options[:left] || nil
@right = options[:right] || nil
end
def children?
@left && @right
end
def no_children?
!children?
end
end
# Binary search tree
def lowest_common_ancestor(node, a, b)
if node.value < a && node.value < b
lowest_common_ancestor(node.right, a, b)
elsif node.value > a && node.value > b
lowest_common_ancestor(node.left, a, b)
else
return node.value
end
end
def lowest_common_ancestor_iteratively(node, a, b)
while node != nil
if node.value < a && node.value < b
node = node.right
elsif node.value > a && node.value > b
node = node.left
else
return node.value
end
end
end
n20 = Node.new({:value => 20})
n8 = Node.new({:value => 8})
n22 = Node.new({:value => 22})
n4 = Node.new({:value => 4})
n12 = Node.new({:value => 12})
n10 = Node.new({:value => 10})
n14 = Node.new({:value => 14})
n12.left = n10
n12.right = n14
n8.left = n4
n8.right = n12
n20.left = n8
n20.right = n22
puts lowest_common_ancestor(n20, 4, 8)
puts lowest_common_ancestor_iteratively(n20, 4, 8)