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 8171785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:34:08+00:00 2026-06-06T21:34:08+00:00

The following is an interview question. You are given a binary tree (not necessarily

  • 0

The following is an interview question.

You are given a binary tree (not necessarily BST) in which each node contains a value.
Design an algorithm to print all paths which sum up to that value.
Note that it can be any path in the tree – it does not have to start
at the root.

Although I am able to find all paths in tree that start at the root have the given sum, I am not able to do so for paths not not starting at the root.

  • 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-06-06T21:34:11+00:00Added an answer on June 6, 2026 at 9:34 pm

    Well, this is a tree, not a graph. So, you can do something like this:

    Pseudocode:

    global ResultList
    
    function ProcessNode(CurrentNode, CurrentSum)
        CurrentSum+=CurrentNode->Value
        if (CurrentSum==SumYouAreLookingFor) AddNodeTo ResultList
        for all Children of CurrentNode
              ProcessNode(Child,CurrentSum)
    

    Well, this gives you the paths that start at the root. However, you can just make a tiny change:

        for all Children of CurrentNode
              ProcessNode(Child,CurrentSum)
              ProcessNode(Child,0)
    

    You might need to think about it for a second (I’m busy with other things), but this should basically run the same algorithm rooted at every node in the tree

    EDIT: this actually gives the “end node” only. However, as this is a tree, you can just start at those end nodes and walk back up until you get the required sum.

    EDIT 2: and, of course, if all values are positive then you can abort the descent if your current sum is >= the required one

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this post , which reports the following interview question: Given two
Interview question: Design a data structure which has the following features push the data
I was given the following as an interview question: class A { public: void
I recently saw an interview question asking the following: Given a 32 bit number,
I got following question on an interview: Which SQL mechanisms allow user to browse
Recently in a job interview I was asked this following question (for Java): Given:
I got following question on an interview: Given a table of natural numbers with
I was asked the following question during phone interview I had: Given the following
I was asked the following Question: How would you store the data given below(which
In the following interview question : Given a number n, give me the numbers

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.