aboutsummaryrefslogtreecommitdiff
path: root/challenge-125
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-08-15 22:49:29 +0200
committerAbigail <abigail@abigail.be>2021-08-15 22:49:29 +0200
commit0f9be85222718e52bf5db9f85c3ab70bc35b29dc (patch)
tree68ab30609b830d3ce5141c25ad7aa848b6de4e46 /challenge-125
parent34aa316cdbeb0e57f530089e366856b7f86b72cc (diff)
downloadperlweeklychallenge-club-0f9be85222718e52bf5db9f85c3ab70bc35b29dc.tar.gz
perlweeklychallenge-club-0f9be85222718e52bf5db9f85c3ab70bc35b29dc.tar.bz2
perlweeklychallenge-club-0f9be85222718e52bf5db9f85c3ab70bc35b29dc.zip
Handle leaves better
Diffstat (limited to 'challenge-125')
-rw-r--r--challenge-125/abigail/perl/ch-2.pl7
1 files changed, 4 insertions, 3 deletions
diff --git a/challenge-125/abigail/perl/ch-2.pl b/challenge-125/abigail/perl/ch-2.pl
index 45806530f7..06006dbe58 100644
--- a/challenge-125/abigail/perl/ch-2.pl
+++ b/challenge-125/abigail/perl/ch-2.pl
@@ -50,8 +50,9 @@ package Tree {
# Get the left and right child of a tree. Leaves obviously
# don't have children.
#
- sub left ($self) {$self && $left {$self}}
- sub right ($self) {$self && $right {$self}}
+ sub left ($self) {!$self -> isleaf && $left {$self}}
+ sub right ($self) {!$self -> isleaf && $right {$self}}
+ sub isleaf ($self) {!$left {$self} || !$right {$self}}
sub diameter ($self) {
($self -> _diameter ($self)) [1]
@@ -67,7 +68,7 @@ package Tree {
# Diameter of a tree is max (diameter left child,
# diameter right child, depth left child + depth right child).
#
- return (0, 0) unless $self; # Leaves have no depth nor diameter.
+ return (0, 0) if $self -> isleaf; # Leaves have no depth nor diameter.
my ($ldp, $ldm) = $self -> left -> _diameter;
my ($rdp, $rdm) = $self -> right -> _diameter;
(max ($ldp, $rdp), max ($ldm, $rdm, $ldp + $rdp))