I’m working on UVa #112 Tree Summing. I have what I think should be a working solution but it is not accepted by the online judge due to a basic misunderstanding of the problem on my part. Consider the following inputs:
-1 (-1()())
77 (77(1()())())
or diagrammatically, the trees look like:
-1 77
/ \ / \
() () 1 ()
/ \
() ()
According to at least two working solutions, the correct output for the above inputs is:
yes
no
However, I don’t understand why the second one should be ‘no’. It looks to me like the rightmost path of the tree should give the proper sum. What am I missing?
Easy.
The tree:
A leaf is of this form: (integer () ())
Thus the tree has only one leaf: (1 () ())
The sum of the nodes to this leaf is 78. 78 is not 77. Result: no.
What you think is a rightmost path, is none according to the definition.
The first tree:
It is just a single leaf node. One path. The sum is -1. -1 = -1. Result: yes.
We can check it with a Lisp program: