Your question, your audience. Choose who sees your identityâ€”and your questionâ€”with question security.

Hi I'm stuck doing this, not sure how to go about it.

If I have two binary trees, how would I check if the have the same shape? Data in the nodes doesn't matter, just if the tree structures are equal.

Any ideas on how to solve this problem?

If I have two binary trees, how would I check if the have the same shape? Data in the nodes doesn't matter, just if the tree structures are equal.

Any ideas on how to solve this problem?

3

/ \

4 23

/

34

and

5

/ \

9 99

/

44

have the same structure. on the other hand

3

/ \

4 23

/

34

and

3

/ \

4 23

/ \

34 55

dont have the same structure.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeMap.html

You could create an algorithm that counted the number of leaf nodes and then checked the length of each leaf.

So the top exampl has 2 leafs with a depth os 3 & 2 : [3,2]

The fourth one has three leafs with depths of [3,3,2]

You could use this signature to determine.

I should not that the solution I gave will get you on the right track but is not the entire solution. I don;t like solving peoples homework. Iwill nudge you in the right direction. If anyone gives you the solution, I hope they employ you caue I'll weed you out. Short cuts only affect you. If you take the time to figure it out you'll be doing yourself no harm, but if you try to short cut the process you will only hurt your learning process.

Good luck.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

You could just start with the first node on each tree; count the number of sub-nodes. If they're the same, then for each sub-node, count their sub-nodes, are they the same in each case? And so on.