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

  • SEARCH
  • Home
  • 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 8402175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:59:06+00:00 2026-06-09T21:59:06+00:00

This is the problem statement: Given a tree and a sum, return true if

  • 0

This is the problem statement:

“Given a tree and a sum, return true if there is a path from the root
down to a leaf, such that adding up all the values along the path
equals the given sum.”

I implemented it like this:

int hasPathSum(Node* node, int sum) {
    if(node == NULL)
        return sum == 0;

    int diff = sum - node->data;

    if(diff < 0) return 0;

    if(diff == 0)
        return node->left == NULL && node->right == NULL ? 1 : 0;

    if(diff < node->data)
        return hasPathSum(node->left, diff);
    else
        return hasPathSum(node->right, diff);
}

I only traverse 1 sub-tree and not both, depending on the difference of (sum - node->data).

However, the recommended solution required traversing both the subtrees like this:

int hasPathSum(Node* node, int sum) { 
  // return true if we run out of tree and sum==0 
  if (node == NULL) { 
    return(sum == 0); 
  } 
  else { 
  // otherwise check both subtrees 
    int subSum = sum - node->data; 
    return(hasPathSum(node->left, subSum) || 
           hasPathSum(node->right, subSum)); 
  } 
}

I don’t see why do we have to traverse both the subtrees ?

Here is the source of the problem: Problem 7

EDIT:

Bad assumption on my part that the tree is a BST. Its just a binary tree.

However, if it were a BST, I’d like to hear some feedback on my implementation ? Would it not be better to do it the way I did it than the other solution ? (which is a little unfair to compare since that solution is for any binary tree)

EDIT solution:

Please see anatolyg’s response here which explains the problem with my proposed solution. Thanks anatolyg.

  • 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-09T21:59:08+00:00Added an answer on June 9, 2026 at 9:59 pm

    Your problem statement doesn’t mention anything about how the tree is arranged, so I don’t appreciate what your left/right test is trying to do. You can’t know anything about the contents of the node->left or node->right subtrees without testing them.

    Also, in the suggested solution the || is going to short-circuit if the left subtree matches, so you are not necessarily traversing both subtrees. You only traverse the entire tree if there is no path: you obviously have to test every path to be sure there isn’t one. (Ok, you also traverse the entire tree if you find a match on the final path…)

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

Sidebar

Related Questions

This is a spin-off of one of my earlier questions Problem statement: Given a
Problem Statement is somewhat like this: Given a website, we have to classify it
Im stuck with the below problem. Problem Statement: Given a non-negative int n, return
This is the problem statement: This is a two player game. Initially there are
The problem statement from leetcode says: Given an array S of n integers, are
The original problem statement is this one: Given an array of 32bit unsigned integers
I'm having some problem with this statement declare @result int select @result = (select
I have a problem when trying to execute this update statement (below) using C#
This problem is same as asked in here . Given a list of coins,
I was practicing SRM Problems in Topcoder.I came across this problem Problem Statement: Today

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.