+ Reply to Thread
Results 1 to 2 of 2

Thread: Find two given trees are same

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

    Find two given trees are same

    You have two binary trees, how will you find that both trees are same. (structurally equal)

  2. #2
    Surfer is offline Senior Member
    Join Date
    Mar 2010
    Posts
    321
    Code:
    struct binTree {
    	int data;
    	struct binTree *left;
    	struct binTree *right;
    };
    
    typedef struct binTree* Tree;
    
    /*
    Given two trees, return true if they are
    structurally identical.
    */
    
    int sameTree(Tree T1, Tree T2) {
    	
    	// 1. both empty -> true
    	if (T1==NULL && T2==NULL) 
    		return(true);
    
    	// 2. both non-empty -> compare them
    	else if (T1!=NULL && T2!=NULL) {
    		return(T1->data == T2->data && sameTree(T1->left, T2->left) && sameTree(T1->right, T2->right) );
    	}
    
    	// 3. one empty, one not -> false
    	else 
    		return(false);
    }

+ 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. Find number of possible unique trees of n nodes
    By TopGun in forum Algorithm/Data Structure Questions
    Replies: 1
    Last Post: 23rd April 2010, 11:10

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