February 21, 2025 |6.7K Views

Symmetric Tree

Explore Courseexplore course icon
Description
Discussion

In this video, we will explore three different approaches to check if a binary tree is symmetric, meaning that it is a mirror image of itself. The first approach uses recursion, where we recursively compare the left and right subtrees of the root node. For the tree to be symmetric, the root values of the left and right subtrees must match, and their corresponding children must also be mirrors. This approach runs in O(n) time and uses O(h) space, where n is the number of nodes in the tree and h is the height of the tree.

The second approach uses two stacks to compare the nodes at each level of the left and right subtrees. By pushing the left child of the root into one stack and the right child into another, we can check if the left and right subtrees are mirror images by comparing nodes from both stacks at each level. This method also runs in O(n) time and uses O(h) space. The third approach employs a queue for level-order traversal, but the concept remains similar to the stack approach. Watch the video to understand these methods in detail and see how they help in determining if a binary tree is symmetric.

For more details, please go through - Symmetric Tree (Mirror Image of itself)