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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:54:12+00:00 2026-06-02T15:54:12+00:00

I have a Node structure as below,this Node has stringdata and list of nodes

  • 0

I have a Node structure as below,this Node has stringdata and list of nodes as it’s children. I wanted to search a data in this tree
I wrote a recursive function FindNode(Node intree,string target)

public class Node
{
   public string Data;
   public List<Node> Children = new List<Node>();
   //some code
   public Node(string r)
   {
        this.Data = r;
   }

   public string getData()
   {
          return this.Data;
   }

   //some code
   public List<Node> getchildren()
   {
         return this.Children;
    }
       //some code
}

target is the string which I want to find and the intree is the begining of the tree(ROOT)
I have problem after while loop what should I return after that?
If I am wrong how should I write it?

public Node FindNode(Node intree,string target)
{
    if(intree.getData()==target)
          return intree;
    else 
    {
         while(intree.getchildren()!=null) 
         {
             foreach(Node n in intree.getchildren())
              {
                   FindNode(n,target);
              }
         }
     }
}
  • 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-02T15:54:14+00:00Added an answer on June 2, 2026 at 3:54 pm

    Use this one:

    public Node FindNode(Node intree,string target)
    {
        if(intree.getData()==target)
              return intree;
        else 
        {
    
                  foreach(Node n in intree.getchildren())
                  {                
                      Node node = FindNode(n,target) ; //CHECK FOR RETURN 
    
                      if(node != null) 
                          return node;            
    
                  }
    
         }
    
         return null;
    }
    

    The difference is that I checked for return of FindNode method, and if it’s not null, return result.

    Just note that in case of dupplicated nodes in the tree (nodes with the same string) it will return first occuarance.

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

Sidebar

Related Questions

I have a tree data structure that is L levels deep each node has
I have a tree structure where each Node has a parent and a Set<Node>
I have the following simple XML structure that has to be parsed: <Nodes> <Node
If i have a tree structure whose nodes can have zero to many children,
I have link list whose node structure is given below struct node { char
This question is more a semantic-algorithmic-data-structure question than a F# syntactically question. I have
Today i was told to create tree data structure with the below class, public
I have a list of items (blue nodes below) which are categorized by the
If I am using a tree structure of nodes similar to the code below,
I have a tree structure and I want to find all nodes matching a

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.