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
75
76
77
78
79
80
|
#!/usr/bin/perl
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-056/
# Task #2
#
# Path Sum
# You are given a binary tree and a sum, write a script to find
# if the tree has a path such that adding up all the values along the path
# equals the given sum. Only complete paths (from root to leaf node)
# may be considered for a sum.
#
# Example
# Given the below binary tree and sum = 22,
#
# 5
# / \
# 4 8
# / / \
# 11 13 9
# / \ \
# 7 2 1
# For the given binary tree, the partial path sum 5 -> 8 -> 9 = 22 is not valid.
#
# The script should return the path 5 -> 4 -> 11 -> 2 whose sum is 22.
use strict;
use warnings;
my $sum = shift || 22;
my $tree = {
v => 5,
l => {
v => 4,
l => {
v => 11,
l => { v => 7 },
r => { v => 2 }
}
},
r => {
v => 8,
l => { v=> 13 },
r => {
v => 9,
r => { v => 1}
}
}
};
travel($tree);
sub travel {
my $node = shift;
my $path = shift // [];
push @$path, $node->{v};
if (1 == scalar keys %$node) {
my $s = 0;
$s += $_ for @$path;
print join(' -> ',@$path).$/ if $s == $sum;
}
else {
foreach my $k ('l','r'){
next unless exists $node->{$k};
travel($node->{$k},$path);
pop @$path;
}
}
}
__END__
./ch-2.pl 22
5 -> 4 -> 11 -> 2
./ch-2.pl 26
5 -> 8 -> 13
|