answersLogoWhite

0


Best Answer

/*

Given two trees, return true if they are

structurally identical.

*/

int sameTree(struct node* a, struct node* b) {

// 1. both empty -> true

if (a==NULL && b==NULL) return(true);

// 2. both non-empty -> compare them

else if (a!=NULL && b!=NULL) {

return(sameTree(a->left, b->left) &&

sameTree(a->right, b->right)

);

}

// 3. one empty, one not -> false

else return(false);

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Two Binary Trees are similar if they are both empty or if they are both non-empty and left and right sub trees are similar Write an algorithm to determine if two Binary Trees are similar?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which search algorithm requires that the arrays contents be sorted?

Binary Search Algorithm


How do you do binary search in file?

A binary search on a random-access file is performed much in the same way as a binary search in memory is performed, with the exception that instead of pointers to items in memory file seek operations are used to locate individual items within the file, then load into memory for further examination. The key aspects of the binary search algorithm do not depend on the specifics of the set of searchable items: the set is expected to be sorted, and it must be possible to determine an order between any two items A and B. Finally, the binary search algorithm requires that the set of searchable items is finite in size, and of a known size.


Which are the searching algorithm always compare the middle element with the searching elements in the given array?

binary search system


How do you print all data in a Binary Search Tree?

By using Depth First Search or Breadth First search Tree traversal algorithm we can print data in Binary search tree.


Algorithm to determine if a binary tree is complete binary?

There are many ways of checking for a complete binary tree. Here is one method:1. Do a level order traversal of the tree and store the data in an array2. If you encounter a nullnode, store a special flag value.3. Keep track of the last non-null node data stored in the array - lastvalue4. Now after the level order traversal, traverse this array up to the index lastvalue and check whether the flag value is encountered. If yes, then it is not a complete binary tree, otherwise it is a complete binary tree.

Related questions

Which search algorithm requires that the arrays contents be sorted?

Binary Search Algorithm


What is the number of nodes in a strictly binary tree which has n leaves?

IF EVERY NON-LEAF NODE IN A BINARY TREE HAS HAS NONEMPTY LEFT AND RIGHT SUBTREES, THE TREE IS TERMED AS A STRICTLY BINARY TREE. SUCH A TREE WITH n LEAVES ALWAYS CONTAINS 2n-1 NODES.


Algorithm to determine if a binary tree is strictly binary?

// Author : SAGAR T.U, PESIT #define TRUE 1 #define FALSE 0 int isStrictBinaryTree (struct tree * n) { if( n NULL ) return TRUE; return FALSE; }


Give you the algorithm of creating a new binary search tree using c?

i want to know how to give the algorithm password in a computer ?


How is a pso program in matlab..how is it created from algorithm..please help?

The PSO or Particle Swarm Optimization Program algorithm in MatLab is created by first creating a binary genetic algorithm.


The order of binary search algorithm is?

n log n - G.Parthiban, SRM


The truncated binary exponential back off algorithm?

This is the Algorithm use by CSMA/CD as a wait period to allow other devices on the network to access the media.


How do you do binary search in file?

A binary search on a random-access file is performed much in the same way as a binary search in memory is performed, with the exception that instead of pointers to items in memory file seek operations are used to locate individual items within the file, then load into memory for further examination. The key aspects of the binary search algorithm do not depend on the specifics of the set of searchable items: the set is expected to be sorted, and it must be possible to determine an order between any two items A and B. Finally, the binary search algorithm requires that the set of searchable items is finite in size, and of a known size.


Why is booth algorithm fast for multiplication two binary numbers?

Shifting in easily accomplished in hardware.


Which are the searching algorithm always compare the middle element with the searching elements in the given array?

binary search system


What has the author Magnus Rattray written?

Magnus Rattray has written: 'An analysis of a genetic algorithm training the binary perception'


How do you print all data in a Binary Search Tree?

By using Depth First Search or Breadth First search Tree traversal algorithm we can print data in Binary search tree.