isSymetricTree
// Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
// For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
// But the following [1,2,2,null,3,null,3] is not:
// YES -- [1,2,2,3,4,4,3]
// 1
// / \
// 2 2
// / \ / \
// 3 4 4 3
// NO -- [1,2,2,null,3,null,3]
// 1
// / \
// 2 2
// \ \
// 3 3
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
// How to solve this problem:
// 1. Check if the tree is empty or has only a root node.
// 2. If not, reverse the right subtree.
// 3. Check if the left and right subtrees are identical.
export default function isSymmetricTree(root) {
if (root === null || (root.right === null && root.left === null)) {
return true;
}
// reverse tree on the right
root.right = revertTree(root.right);
return isSameTree(root.left, root.right);
}
// Function to reverse tree
function revertTree(node) {
if (node === null || (node.left === null && node.right === null)) {
return node;
}
const temp = revertTree(node.left); // temp -> left (reverse child)
node.left = revertTree(node.right); // left -> right (reverse child)
node.right = temp; // right -> temp
return node;
}
// function checking similar tree
function isSameTree(left, right) {
if (left === null && right === null) {
return true;
}
if (left === null || right === null) {
return false;
}
if (left.val !== right.val) {
return false;
}
return isSameTree(left.right, right.right) && isSameTree(left.left, right.left);
}