-
Notifications
You must be signed in to change notification settings - Fork 1
/
sum_of_all_left_leavies_in_binary_tree.rb
65 lines (53 loc) · 1.56 KB
/
sum_of_all_left_leavies_in_binary_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 'pry'
class Node
attr_accessor :value, :left, :right, :name, :parent
def initialize(options={})
@value = options[:value]
@name = options[:name]
@left = options[:left] || nil
@right = options[:right] || nil
end
def children?
@left && @right
end
def left=(left)
@left = left
@left.parent = self
end
def right=(right)
@right = right
@right.parent = self
end
def no_children?
!children?
end
end
def sum_of_all_left_leavies_in_binary_tree(node)
queue = [node]
value = 0
while queue.any?
e = queue.pop
value += e.value if e.parent && e == e.parent.left && !e.left
queue << e.left if e.left
queue << e.right if e.right
end
value
end
root = Node.new({:value => 20, :name => 'root'})
child_1 = Node.new({:value => 9, :name => 'child_1'})
child_2 = Node.new({:value => 49, :name => 'child_2'})
grand_child_1 = Node.new({:value => 5, :name => 'grand_child_1'})
grand_child_2 = Node.new({:value => 12, :name => 'grand_child_1'})
grand_child_3 = Node.new({:value => 23, :name => 'grand_child_1'})
grand_child_4 = Node.new({:value => 52, :name => 'grand_child_1'})
great_grand_child_5 = Node.new({:value => 15, :name => 'grand_child_1'})
great_grand_child_6 = Node.new({:value => 50, :name => 'grand_child_1'})
child_1.left = grand_child_1
child_1.right = grand_child_2
child_2.left = grand_child_3
child_2.right = grand_child_4
root.left = child_1
root.right = child_2
grand_child_2.right = great_grand_child_5
grand_child_4.left = great_grand_child_6
puts sum_of_all_left_leavies_in_binary_tree(root)