Two Trees Mirror OR Identical

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 177:-Two Trees Mirror OR Identical

You can determine whether two trees are mirrors of each other or identical by performing tree traversals and comparing the structures of the trees. In a mirrored relationship, the left subtree of one tree should be the mirror image of the right subtree of the other tree.

Here's a Python implementation to check if two trees are mirrors of each other or identical:

pythonCopy code

class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def are_mirror_trees(root1, root2): if root1 is None and root2 is None: return True if root1 is None or root2 is None: return False return (root1.data == root2.data) and \ are_mirror_trees(root1.left, root2.right) and \ are_mirror_trees(root1.right, root2.left) def are_identical_trees(root1, root2): if root1 is None and root2 is None: return True if root1 is None or root2 is None: return False return (root1.data == root2.data) and \ are_identical_trees(root1.left, root2.left) and \ are_identical_trees(root1.right, root2.right) # Example usage tree1 = TreeNode(1) tree1.left = TreeNode(2) tree1.right = TreeNode(3) tree1.left.left = TreeNode(4) tree1.left.right = TreeNode(5) tree2 = TreeNode(1) tree2.left = TreeNode(3) tree2.right = TreeNode(2) tree2.right.left = TreeNode(5) tree2.right.right = TreeNode(4) identical = are_identical_trees(tree1, tree2) if identical: print("The two trees are identical.") else: print("The two trees are not identical.") mirrors = are_mirror_trees(tree1, tree2) if mirrors: print("The two trees are mirrors of each other.") else: print("The two trees are not mirrors of each other.")

In this example, the TreeNode class represents nodes of the binary tree. The are_mirror_trees function checks if two trees are mirrors of each other, while the are_identical_trees function checks if two trees are identical. Both functions use recursive traversal and comparison to determine the relationship between the trees.

The key idea behind are_mirror_trees is that the left subtree of one tree should mirror the right subtree of the other tree, and vice versa.

The are_identical_trees function compares the nodes of the trees and recursively checks the left and right subtrees for identical structures.

Keep in mind that these implementations assume that the trees are binary trees. If you're dealing with more general tree structures (not just binary trees), the logic would need to be adjusted accordingly.

25. Tree - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?