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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw'say state signatures';
no warnings qw'experimental::signatures';
# TASK #1 › Binary Tree Depth
# Submitted by: Mohammad S Anwar
#
# You are given binary tree.
#
# Write a script to find the minimum depth.
#
# The minimum depth is the number of nodes from the root to the nearest leaf node (node without any children).
#
# Example 1:
#
# ```
# Input: '1 | 2 3 | 4 5'
#
# 1
# / \
# 2 3
# / \
# 4 5
#
# Output: 2
# ```
#
#
# ```
# Example 2:
#
# Input: '1 | 2 3 | 4 * * 5 | * 6'
#
# 1
# / \
# 2 3
# / \
# 4 5
# \
# 6
# Output: 3
# ```
run() unless caller();
sub run() {
my ($input) = @ARGV;
my @tokens = tokenize($input);
say minimum_binary_tree_depth(@tokens);
}
sub minimum_binary_tree_depth ( @tokens) {
my $depth = 0;
my $tree = [];
while (@tokens) {
push @$tree, [];
my $num_elems = 2**$depth;
for ( my $i = 0 ; $i < $num_elems ; $i++ ) {
if ( !@tokens || $tokens[0]->isa('SeparatorToken') ) {
## fill row with dummy placeholder tokens.
unshift @tokens,
map { PlaceHolderToken->new(-1) }
0 .. ( $num_elems - 1 - $i ); # Dummy Token
}
my $cur = shift @tokens;
if ( $cur->isa('ValueToken') ) {
if ( $depth && !defined( $tree->[-2][ int( $i / 2 ) ] ) ) {
die join( " ",
"Missing parent for node with value",
$cur->{lexeme},
"at position",
$cur->pos_human_readable(),
"in input\n" );
}
push @{ $tree->[-1] }, $cur->{lexeme};
}
elsif ( $cur->isa('PlaceHolderToken') ) {
if ( $i % 2
&& !defined $tree->[-1][-1]
&& ( !$depth || defined $tree->[-2][ int( $i / 2 ) ] ) )
{
return $depth;
}
push @{ $tree->[-1] }, undef;
## do nothing
}
}
$depth += 1;
# handle optional separatortoken
if ( @tokens && $tokens[0]->isa("SeparatorToken") ) {
shift @tokens;
}
}
return $depth;
}
sub tokenize ( $input) {
my @tokens;
my $pos = 0;
while ( $pos < length $input ) {
my $cur = substr( $input, $pos, 1 );
if ( $cur =~ m/\|/ ) {
push @tokens, SeparatorToken->new($pos);
$pos += 1;
}
elsif ( $cur =~ m/\*/ ) {
push @tokens, PlaceHolderToken->new($pos);
$pos += 1;
}
elsif ( $cur =~ m/\d/ ) {
my $start_pos = $pos;
my $lexeme = '';
do {
$lexeme .= $cur;
$pos += 1;
$cur = substr( $input, $pos, 1 );
} while ( $cur =~ m/\d/ );
push @tokens, ValueToken->new( $lexeme, $pos );
}
elsif ( $cur =~ m/\s/ ) {
do {
$pos += 1;
$cur = substr( $input, $pos, 1 );
} while ( $cur =~ m/\s/ );
}
else {
my $pos_human_readable = $pos + 1;
die "Unexpected input at position $pos_human_readable: '$cur'\n";
}
}
return @tokens;
}
package TokenType {
sub new ( $class, $lexeme, $pos ) {
return bless { lexeme => $lexeme, position => $pos }, $class;
}
sub pos_human_readable($self) {
return $self->{position} + 1;
}
}
package ValueToken {
use base 'TokenType';
}
package PlaceHolderToken {
use base 'TokenType';
sub new ( $class, $pos ) {
return bless { lexeme => '*', position => $pos }, $class;
}
}
package SeparatorToken {
use base 'TokenType';
sub new ( $class, $pos ) {
return bless { lexeme => '|', position => $pos }, $class;
}
}
|