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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You can easily do that with recursion. This following code works because two non-empty trees have the same shape if and only if their respective subtrees have the same shape.
To check two trees, pass their root nodes to the function above.