Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 669511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:09:32+00:00 2026-05-14T00:09:32+00:00

I’m working on UVa #112 Tree Summing . I have what I think should

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:09:33+00:00Added an answer on May 14, 2026 at 12:09 am

    Easy.

    The tree:

    (77(1()())())
    

    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:

    (-1()())
    

    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:

    (defun leaf-p (node)
      "is the node a leaf?"
      (and (= (length node) 3)
           (integerp (first node))
           (null (second node))
           (null (third node))))
    
    (defun path-sums (tree)
      "returns a list of all sums for each path"
      (if (leaf-p tree)
          (list (first tree))
        (mapcar (lambda (s)
                  (+ (first tree) s))
                (append (when (second tree)
                          (path-sums (second tree)))
                        (when (third tree)
                          (path-sums (third tree)))))))
    
    (defun tree-sum-p (sum tree)
      (member sum (path-sums tree)))
    
    CL-USER 223 > (path-sums '(-1()()))
    (-1)
    
    CL-USER 224 > (path-sums '(77(1()())()))
    (78)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.