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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:00:33+00:00 2026-05-15T15:00:33+00:00

Calculate the longest path between two nodes. The path is in an arch. Signature

  • 0

Calculate the longest path between two nodes.
The path is in an arch.
Signature of method is:

public static int longestPath(Node n)

In the example binary tree below, it is 4 (going thru 2-3-13-5-2).

This is what I have right now and for the given tree it just returns 0.

public static int longestPath(Node n) {
    if (n != null) {
        longestPath(n, 0);
    }
    return 0;
}
private static int longestPath(Node n, int prevNodePath) {

    if (n != null && n.getLeftSon() != null && n.getRightSon() != null) {
        int currNodePath = countLeftNodes(n.getLeftSon()) + countRightNodes(n.getRightSon());
        int leftLongestPath = countLeftNodes(n.getLeftSon().getLeftSon()) + countRightNodes(n.getLeftSon().getRightSon());
        int rightLongestPath = countLeftNodes(n.getRightSon().getLeftSon()) + countRightNodes(n.getRightSon().getRightSon());

        int longestPath = currNodePath > leftLongestPath ? currNodePath : leftLongestPath;
        longestPath = longestPath > rightLongestPath ? longestPath : rightLongestPath;

        longestPath(n.getLeftSon(), longestPath);
        longestPath(n.getRightSon(), longestPath);

        return longestPath > prevNodePath ? longestPath : prevNodePath;
    }
    return 0;
}
private static int countLeftNodes(Node n) {
    if (n != null) {
        return 1+ countLeftNodes(n.getLeftSon());
    }
    return 0;
}
private static int countRightNodes(Node n) {
    if (n != null) {
        return 1+ countRightNodes(n.getRightSon());
    }
    return 0;
}

I understand that I’m missing a key concept somewhere… My brain goes crazy when I try tracking the flow of execution…
Am I right by saying that by finding the longest path among the root, its left & right nodes and then recurse on its left & right nodes passing them the longest path from previous method invocation and finally (when?) return the longest path, I’m not certain as to how you go about returning it…

  • 1 1 Answer
  • 1 View
  • 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-15T15:00:34+00:00Added an answer on May 15, 2026 at 3:00 pm

    Maybe it is just as simple:

    public static int longestPath(Node n) {
        if (n != null) {
            return longestPath(n, 0); // forgot return?
        }
        return 0;
    }
    

    Its more complicated than one might think at first sight. Consider the following tree:

          1
         / \
        2   3
       / \
      4   5
     / \   \
    6   7   8
       / \   \
      9   a   b
    

    In this case, the root node is not even in the longest path (a-7-4-2-5-8-b).

    So, what you must do is the following: For each node n you must compute the following:

    • compute longest path in left subtree starting with the root of the left subtree (called L)
    • compute longest path in right subtree starting with the root of the right subtree (called R)
    • compute the longest path in left subtree (not necessarily starting with the root of the left subtree) (called l)
    • compute the longest path in right subtree (not necessarily starting with the root of the right subtree) (called r)

    Then, decide, which combination maximizes path length:

    • L+R+2, i.e. going from a subpath in left subtree to current node and from current node through a subpath in right subtree
    • l, i.e. just take the left subtree and exclude the current node (and thus right subtree) from path
    • r, i.e. just take the right subtree and exclude the current node (and thus left subtree) from path

    So I would do a little hack and for every node not return just a single int, but a triple of integers containing (L+R+2, l, r). The caller then must decide what to do with this result according to the above rules.

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

Sidebar

Related Questions

How do I calculate the distance between two points specified by latitude and longitude?
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
Requirements: Calculate the number of months between two dates: receiveDate and dueDate. Both optimistic
I am trying to calculate the difference between given two times and i find
I need to calculate the difference between two timestamps in milliseconds. Unfortunately, the DateDiff-function
I am trying to calculate the distance between two points (Pins) on the iPhone
I want to calculate total say Sundays,Mondays...Saturdays between two days. I want a do
How do you calculate the distance between 2 cities?
I want to calculate the time span between 2 times which I saved in
I'm looking for the best way to calculate a nodes balance in an AVL-tree

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.