Solution: Symmetric Tree

Let's solve the Symmetric Tree problem using the Tree BFS pattern.

Statement#

Given the root of a binary tree, check whether it is a symmetric tree. A symmetric tree refers to a tree that is the mirror of itself, i.e., symmetric around its root.

Constraints:

  • The tree contains nodes in the range [1,500][1, 500].
  • 103-10^3 \le Node.data 103\le 10^3

Solution#

Starting from the root, we have to check that the left and right subtrees of the tree are mirrors of each other. The tree containing a single element root is always symmetric; we will return TRUE in this case. Otherwise, we will use the following algorithm:

  1. We take a queue with root's right and left nodes.
  2. Iterate a loop till the queue is not empty.
  3. In each iteration, we dequeue two elements from the queue and store them in two variables, left and right.
    • If both left and right are NULL, it means that the node doesn’t have any child. So, we move to the next interaction.
    • If any from left and right is NULL, it means that either the left child or right child of the node doesn’t exist, which leads us to the conclusion that the tree is not symmetric; we return FALSE.
    • If the values of left and right are different, it also means that the tree is not symmetric; we return FALSE.
  4. Then, we append the left node of left, the right node of right, the left node of right, and the right node of left in the queue in the same order, and we move to the next interaction.
  5. After completing all interactions, if the algorithm is not returned from any step, this indicates that the tree is symmetric, so we return TRUE.

Let’s look at the following illustration to get a better understanding of the solution:

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: 2 2 left - right - Create a queue and add the root's left (2) and the root's right (2) in thequeue.

1 of 10

Created with Fabric.js 3.6.6 Queue: left 2 right 2 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null 3 3 Dequeue two elements (2, 2) from the queue and store in the left andthe right variables. Since the node values of left and right are the same,enqueue the left node of left (3) and the right node of right (3)in the queue.

2 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: 3 3 4 4 left 2 right 2 Now, enqueue the right node of left (4) and the left node of right (4)in the queue.

3 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: 4 4 Null Null Null Null left 3 right 3 Dequeue two elements (3, 3) from the queue and store in the left andthe right variables. Since the node values of left and right are the same,enqueue the left node of left (NULL), the right node of right (NULL),the right node of left (NULL), and the left node of right (NULL) in the queue.

4 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: Null Null Null Null Null Null Null Null left 4 right 4 Dequeue two elements (4, 4) from the queue and store in the left andthe right variables. Since the node values of left and right are the same,enqueue the left node of left (NULL), the right node of right (NULL),the right node of left (NULL), and the left node of right (NULL) in queue.

5 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: Null Null Null Null Null Null left null right null Dequeue two elements (NULL, NULL) from the queue and store in the leftand right variables. Since the left and right nodes are NULL, moveto the next iteration.

6 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: Null Null Null Null left null right null Dequeue two elements (NULL, NULL) from the queue and store in the leftand right variables. Since the left and right nodes are NULL, moveto the next iteration.

7 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: Null Null left null right null Dequeue two elements (NULL, NULL) from the queue and store in the leftand right variables. Since the left and right nodes are NULL, moveto the next iteration.

8 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: EMPTY left null right null Dequeue two elements (NULL, NULL) from the queue and store in the leftand right variables. Since the left and right nodes are NULL, moveto the next iteration.

9 of 10

Created with Fabric.js 3.6.6 1 2 2 3 4 4 3 Null Null Null Null Null Null Null Null Queue: EMPTY left null right null Since the queue is empty, it indicates that the tree is symmertic.Return TRUE.

10 of 10

Let’s have a look at the code for the algorithm we just discussed.

main.py
TreeNode.py
BinaryTree.py
Symmetric Tree

Note: This can also be achieved using a stack instead of a queue.

Time complexity#

The time complexity of this solution is O(n)O(n), where nn is the total number of nodes in the tree. This is because we traverse the entire tree once.

Space complexity#

Additional space is required to store a maximum of nn elements in the queue, where nn is the total number of nodes in the tree. So, the space complexity of this solution is O(n)O(n).

Symmetric Tree

Vertical Order Traversal of a Binary Tree