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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:33:32+00:00 2026-06-11T09:33:32+00:00

I came up with this code but it requires a global variable Rank .

  • 0

I came up with this code but it requires a global variable Rank. Is there any way I can solve this problem without having to have a global variable?

int Rank = 0; 
public int inOrderTraversal(TreeNode node, int n){
    if(node==null)
        return 0;
    int x=inOrderTraversal(node.left,n);
    if(x!=0)return x;
    Rank++;
    if(n==Rank) return node.data;
    int y=inOrderTraversal(node.right,n);
    int c= x==0 ? y:x;
    return c;
}

I am just trying to return the nth term in an in-order traversal of a binary tree.

  • 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-11T09:33:33+00:00Added an answer on June 11, 2026 at 9:33 am

    You can pass a TraversalState object down the recursion invocation chain, and store the number of nodes that you visited in a variable there:

    class TraversalState {
        public int rank = 0;
    }
    ...
    public int inOrderTraversal(TreeNode node, int n, TraversalState ts){
        if(node==null)
            return 0;
        int x=inOrderTraversal(node.left,n, ts);
        ts.rank++;
        if(n==ts.rank) return node.data;
        int y=inOrderTraversal(node.right,n, ts);
        int c= x==0 ? y:x;
        return c;
    }
    

    Now your implementation is thread-safe, because it does not use “global” objects. Invoke it as follows:

    int r = inOrderTraversal(myNode, targetN, new TraversalState());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried to compile Ercia Sadun's sample code here , but this error came
This is a not related to my project code , but a question came
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came accross this code on the web: is_char(Ch) -> if Ch < 0
I came across this code on reddit . I would have thought that type
in the book i'm learning from i came across this code snippit: while (i
I recently came along this line of code: CustomData_em_free_block(&em->vdata, &eve->data); And I thought, isn't:
I just came a cross this nice code that makes this scatter matrix plot:
I am using this code to check if the request came from a page
I was look at somebody else's code and came across this piece of code

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.