+ Reply to Thread
Results 1 to 2 of 2

Thread: Find level of an element in a tree

  1. #1
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321

    Find level of an element in a tree

    Write a c program to search for an element in a binary tree[not BST] and if found return its level. root is labelled level 0 and children are one level incremented than its parent.
    if not found return -1

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Code:
    int findLevel(TREE * t, int& levelCount, int data)
    {
    	int left,right,temp;
    	if(!t)
    		return -1;
    	if(t->data == data ) {
    		levelCount=0;
    		return 1;
    	} else {
    		left=findLevel(t->left,levelCount,data);
    		right= findLevel(t->right,levelCount,data);
    		temp=(left == -1 ) ? right: left;
    		levelCount++;
    		return temp;
    	}
    }

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Print Binary Tree data level by level
    By TopGun in forum Amazon
    Replies: 1
    Last Post: 2nd July 2010, 04:33
  2. Level order printing of a tree
    By Surfer in forum Goldman Sachs
    Replies: 0
    Last Post: 26th May 2010, 23:53
  3. Find 4th smallest element of a tree
    By TopGun in forum Adobe
    Replies: 2
    Last Post: 22nd April 2010, 23:31

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts