-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_common_ancestor.rb
74 lines (58 loc) · 1.47 KB
/
find_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
63
64
65
66
67
68
69
70
71
72
73
74
require 'pry'
require 'pp'
class Node
attr_accessor :value, :left, :right
def initialize(options={})
@value = options[:value]
@name = options[:name]
@left = options[:left]
@right = options[:right]
end
def children?
@left && @right
end
def no_children?
@left.nil? && @right.nil?
end
def right
@right || Node.new
end
def left
@left || Node.new
end
end
def lca(root, p, q)
return nil if root.value == nil
return root if p == root || q == root
left = lca(root.left, p, q)
right = lca(root.right, p, q)
return root if left && right
left ? left : right
end
n10 = Node.new({:value => 10})
n5 = Node.new({:value => 5})
n15 = Node.new({:value => 15})
n3 = Node.new({:value => 3})
n12 = Node.new({:value => 12})
n9 = Node.new({:value => 9})
n13 = Node.new({:value => 13})
n12.left = n9
n12.right = n13
n5.left = n3
n5.right = n12
n10.left = n5
n10.right = n15
pp lca(n10, n3, n15).value
pp lca(n10, n3, n12).value
pp lca(n10, n3, n9).value
pp lca(n10, n3, n12).value
pp lca(n10, n13, n15).value
def lca_with_path(root, p, q, array=[])
return [nil, array] if root.value == nil
return [root, array.dup << root] if p == root || q == root
left, array = lca_with_path(root.left, p, q, array)
right, array = lca_with_path(root.right, p, q, array)
return [root, array.dup << root] if left && right
left ? [left, array.dup << left] : [right, array.dup << right]
end
pp lca_with_path(n10, n3, n15, [])[1].compact.map(&:value)