blob: 3c709f59be3619b0ca167243cd04b9a169c09e77 (
plain)
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
|
#!/usr/bin/env perl
# Challenge 113
#
# TASK #2 - Recreate Binary Tree
# Submitted by: Mohammad S Anwar
# You are given a Binary Tree.
#
# Write a script to replace each node of the tree with the sum of all the
# remaining nodes.
#
# Example
# Input Binary Tree
# 1
# / \
# 2 3
# / / \
# 4 5 6
# \
# 7
# Output Binary Tree
# 27
# / \
# 26 25
# / / \
# 24 23 22
# \
# 21
use Modern::Perl;
my $tree = join('', <>);
my $sum = 0;
$sum += $1 while $tree =~ /(\d+)/g;
$tree =~ s/(\d+) ?/$sum - $1/ge;
print $tree;
|