diff options
| author | Joel Crosswhite <joel.crosswhite@ix.netcom.com> | 2021-01-03 13:41:44 -0700 |
|---|---|---|
| committer | Joel Crosswhite <joel.crosswhite@ix.netcom.com> | 2021-01-03 13:41:44 -0700 |
| commit | feba6c2d507d20851aa10ee41ec9f16530e70271 (patch) | |
| tree | aecc339c9398b028d6ca9029e6f7810563f7cdef /challenge-093 | |
| parent | ecf2b24e7e08f5562a378ae466cbc3a992463e53 (diff) | |
| download | perlweeklychallenge-club-feba6c2d507d20851aa10ee41ec9f16530e70271.tar.gz perlweeklychallenge-club-feba6c2d507d20851aa10ee41ec9f16530e70271.tar.bz2 perlweeklychallenge-club-feba6c2d507d20851aa10ee41ec9f16530e70271.zip | |
Added solution for challenge #2.
Diffstat (limited to 'challenge-093')
| -rw-r--r-- | challenge-093/jcrosswh/perl/ch-2.pl | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/challenge-093/jcrosswh/perl/ch-2.pl b/challenge-093/jcrosswh/perl/ch-2.pl new file mode 100644 index 0000000000..7bc1209df8 --- /dev/null +++ b/challenge-093/jcrosswh/perl/ch-2.pl @@ -0,0 +1,73 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Tree::Binary; + +=head1 NAME + +PWC 093 Challenge 2 + +=head1 SYNOPSIS + + $ ch-2.pl + 32 + +=head1 DESCRIPTION + +Given a binary tree containing numbers 0-9 only, this script will sum all +possible paths from root to leaf. + +=head1 SOLUTION + +For this solution, I did use the library Tree::Binary in order to provide quick +setup of a tree and a node object that would allow me to traverse the left and +right nodes. This solution also does not allow for input to dynamically build +a tree (I couldn't see a good way of inputting data that would build a good +tree.) + +Once the tree is built, a recursive method is called passing in a stack (as an +array) and the current node. If this node is a leaf (the exit condition), then +we'll sum the current nodes value with all values in the stack and return that +total. Otherwise, we'll push the current node value to the stack and try the +left and right branches, if they exist. The total will be taken from the return +value. + +=head1 AUTHORS + +Joel Crosswhite E<lt>joel.crosswhite@ix.netcom.comE<gt> + +=cut + +my ($btree) = Tree::Binary -> new('1') + -> setLeft + ( + Tree::Binary -> new('0') + -> setLeft(Tree::Binary->new('2') ) + ) + -> setRight + ( + Tree::Binary->new('9') + -> setLeft(Tree::Binary->new('4') ) + -> setRight(Tree::Binary->new('5') ) + ); + +print add_paths([], $btree) . "\n"; +exit 0; + +sub add_paths { + my ($stack, $node) = @_; + + my $total = 0; + if ($node->isLeaf()) { + my $total = $node->getNodeValue(); + map { $total += $_; } @{$stack}; + return $total; + } else { + push(@{$stack}, $node->getNodeValue()); + $total += add_paths($stack, $node->getLeft()) if $node->hasLeft(); + $total += add_paths($stack, $node->getRight()) if $node->hasRight(); + pop(@{$stack}); + } + return $total; +} |
