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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:31:52+00:00 2026-05-31T03:31:52+00:00

I know how to find if a binary tree has a certain path with

  • 0

I know how to find if a binary tree has a certain path with a given sum (If this is not the best way please let me know):

int pathSum(MyNode root, int sum)
{
    if(root == null)
        return -1;
    int temp = sum - root.value;
    return(pathSum(root.left,temp) || pathSum(root.right,temp));
}

What I am not able to figure out is how to print the particular path.

My Node class looks like this:

class MyNode {
    int value;
    MyNode left;
    MyNode right;

    MyNode(int value)
    {
        this.value = value;
    }
}
  • 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-31T03:31:53+00:00Added an answer on May 31, 2026 at 3:31 am

    Try this, use overloading:

     public void pathToSum(int sum) {
        pathToSum(root, sum);
    }
    
    private boolean pathToSum(Node n, int sum) {
        if (null != n) {
            sum -= n.data;
            boolean found = pathToSum(n.left, sum);
    
            if (!found) {
                found = pathtoSum(n.right, sum);
            }
            if (found) {
                println(n.data);
                                return found;
            }
        }
        return 0 == sum ? true : false;
    }
    

    This code is tested with the following classes:

    import java.util.LinkedList;
    import java.util.Queue;
    public class BST {
    Node root;
    public BST(){
        root = null;
    }
    
    public void insert(int el){
    
        Node tmp = root, p=null;
        while(null!=tmp && el != tmp.data){
            p=tmp;
            if(el<tmp.data)
                tmp=tmp.left;
            else
                tmp=tmp.right;
        }
        if(tmp == null){
            if(null == p)
                root = new Node(el);
            else if(el <p.data)
                p.left= new Node(el);
            else
                p.right=new Node(el);
        }
    }//
    
    public void pathToSum(int sum) {
        pathToSum(root, sum);
    }//
    
    private boolean pathToSum(Node n, int sum) {
        if (null != n) {
            sum -= n.data;
            boolean found = pathToSum(n.left, sum);
    
            if (!found) {
                found = pathToSum(n.right, sum);
            }
            if (found) {
                System.out.println(n.data);
                return found;
            }
        }
        return 0 == sum ? true : false;
    }
    
    public static void main(String[] args){
        int[] input={50,25,75,10,35,60,100,5,20,30,45,55,70,90,102};
        BST bst = new BST();
        for(int i:input)
            bst.insert(i);
        bst.pathToSum(155);
    }
    }
    
    class Node{
    public int data;
    public Node left;
    public Node right;
    public Node(int el){
        data = el;
    }
    }
    

    Result:

    45
    35
    25
    50
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw this problem: Given a binary search tree and a number, find a
I know I can use this way to find the most frequent element in
Do you know, please, if C++ STL contains a Binary Search Tree (BST) implementation,
Given this binary tree (actually, the binary tree can be random and dynamic, this
I want know what is the best : Array OR Binary search tree in
The only way I know is: find /home -xdev -samefile file1 But it's really
Anyone know of a way to find out the amount of memory/size of a
Looking through the source code of a binary tree,I find the following function: //definition
I know that this is discussed a lot but I don't find solution of
Does anybody know of a (common case) faster-than-linear way to find the endpoints of

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.