Check Symmetrical Binary Tree using JavaScript
Last Updated :
14 Jun, 2024
Given a binary tree, our task is to check whether it is Symmetrical Binary tree. In other words, we need to check whether the binary tree is a mirror of itself.
Example:
Input: 11
/ \
12 12
/ \ / \
13 14 14 13
Output: True
Input: 11
/ \
12 12
\ \
13 13
Output: False
Below are the approaches to check Symmetrical Binary tree in JavaScript:
Recursive Approach
In this approach we recursively check if the left subtree is a mirror of the right subtree. This means we first compare the root values of the subtrees, and then we check if the left child of one subtree is equal to the right child of the other subtree, and vice versa. If all these checks hold true for every node, the tree is symmetric.
Example: To demonstrate Checking Symmetrical Binary tree using recursive approach.
JavaScript
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// Function to check if two
// subtrees are mirrors of each other
function isMirror(left, right) {
if (left === null && right === null) {
return true;
}
if (left === null || right === null) {
return false;
}
return (left.val === right.val) &&
isMirror(left.left, right.right) &&
isMirror(left.right, right.left);
}
// Function to check if a tree is symmetric
function isSymmetric(root) {
if (root === null) {
return true;
}
return isMirror(root.left, root.right);
}
// Example usage:
let root = new TreeNode(11);
root.left = new TreeNode(12);
root.right = new TreeNode(12);
root.left.left = new TreeNode(13);
root.left.right = new TreeNode(14);
root.right.left = new TreeNode(14);
root.right.right = new TreeNode(13);
console.log(isSymmetric(root));
Time Complexity: O(n), where n is the number of nodes in the tree.
Auxiliary Space: O(h), where h is the height of the tree due to the recursive call stack.
Iterative approach
In this approach we use queue to perform a level-order traversal of the tree. First, we enqueue the left and right children of the root. Then, for each pair of nodes dequeued, we compare their values. If they are not equal, the tree is not symmetric. If they are equal, we enqueue their children in the order needed to check symmetry (left child of the first node with right child of the second node, and right child of the first node with left child of the second node). If we successfully process all nodes without finding any asymmetry, the tree is symmetric.
Example: To demonstrate Checking Symmetrical Binary tree using Iterative approach.
JavaScript
class TreeNode {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// Function to check if a tree is
// symmetric using iterative approach
function isSymmetric(root) {
if (root === null) {
return true;
}
let queue = [];
queue.push(root.left);
queue.push(root.right);
while (queue.length > 0) {
let left = queue.shift();
let right = queue.shift();
if (left === null && right === null) {
continue;
}
if (left === null || right === null
|| left.val !== right.val) {
return false;
}
queue.push(left.left);
queue.push(right.right);
queue.push(left.right);
queue.push(right.left);
}
return true;
}
// Example usage:
let root = new TreeNode(11);
root.left = new TreeNode(12);
root.right = new TreeNode(12);
root.left.left = new TreeNode(13);
root.left.right = new TreeNode(14);
root.right.left = new TreeNode(14);
root.right.right = new TreeNode(13);
console.log(isSymmetric(root));
Time Complexity: O(n), where n is the number of nodes in the tree.
Auxiliary Space: O(n), for the queue used in the level-order traversal.
Similar Reads
Boundary Traversal of Binary Tree using JavaScript The Boundary Traversal of the Binary Tree can be done by traversing the left, right, and leaf parts of the Binary Tree. We will break the Boundary Traversal of Binary Tree using JavaScript in three parts. First, we will traverse the left part, right part, and leaf parts of the Binary Tree and print
4 min read
Convert a Binary Tree to a Binary Search Tree using JavaScript Given a Binary Tree, the task is to convert it to a Binary Search Tree. The conversion must be done in such a way that it keeps the original structure of the Binary Tree. Example: To demonstrate the conversion of the Binary Tree to the Binary Search Tree.Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 /
2 min read
Merge Two Balanced Binary Search Trees using JavaScript We are given two different Balanced Binary Search Trees and we have to merge them to form a single balanced binary search tree. A Balanced Binary Search Tree (BST) is a binary search tree that keeps its height close to the minimum possible height. This is achieved by ensuring that the height differe
4 min read
Count the Number of Nodes in a Complete Binary tree using JavaScript We need to count the number of the nodes in the complete binary tree using JavaScript. The complete binary tree is a binary tree in which every level then except possibly the last, is completely filled and all the nodes are as far left as possible. There are several approaches for counting the numbe
4 min read
Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin
3 min read
Print the nodes having exactly one child in a Binary tree using JavaScript Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return â-1â if no such node exists. Examples: Input: 1 / \ 2 3 / \ 4 5 / 6Output: 3Explanation:There are three nodes in the Binary tree that have at least one child that are 1,2,4
4 min read
Symmetric Binary Tree Given a binary tree, check whether it is a mirror of itself. Examples: Input: 5 / \ 3 3 / \ / \ 8 9 9 8 Output: Symmetric Input: 5 / \ 8 7 \ \ 4 3 Output: Not Symmetric Approach: The idea is to traverse the tree using Morris Traversal and Reverse Morris Traversal to traverse the given binary tree an
9 min read
Find the Distance Between Two Nodes in the Binary Tree using JavaScript Given two nodes, our task is to find the distance between two nodes in a binary tree using JavaScript, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. Examples: Input: Binary Tree as described above in diagra
5 min read
JavaScript Program to Find Maximum Width of Binary Tree The maximum width of a given binary tree can be calculated by finding the maximum of all the level widths. The maximum width of a Binary tree is the maximum number of nodes present at any level. The maximum width of the Binary tree will include only the present node and not include null nodes. The m
4 min read
Inverting a Binary Tree in JavaScript One can Invert a binary tree using JavaScript. Inverting a binary tree, also known as mirroring a binary tree, is the process of swapping the left and right children of all nodes in the tree. Below is the diagram to understand the problem clearlyInverting a binary treeBelow are the approaches to inv
5 min read