Solving Symmetric Tree Algorithm problem in JavaScript

Continuing on with working on algorithm problems on LeetCode, the following problem was a bit tricky for me to solve. I was spinning in circles trying to find the appropriate answer, until I found the solution thanks to the Terrible Whiteboard YouTube channel. To help me retain what I learned, I decided to write down the solution and explain it the best way I can.

var isSymmetric = function(root) {};

To start off, I like changing the function to the standard JavaScript ES6 format. So I use const in order to define the function and I use the arrow function for our setup.

const isSymmetric =(root)=> {

};

Example 1:

Step #1: First we want to set up the conditional statement for when root === null we return true. The root in our case is the array we have [1,2,2,3,4,4,3]. So if the array has no amount or is null, our function will return null.

const isSymmetric =(root)=> {
if (root === null) {
return true;
}
};

Step #2: Our next step is to create a separate function in order to deal with comparing two trees to see if they are mirrored. We will call them tree1 and tree2. Our overall function that we will name for this is isMirror.

const isMirror = (tree1, tree2) => {
// if either node is null, we are at a leaf node. either they are the same, in which case we can return true

// if one is null and the other is not, we can return false
if (tree1 === null || tree2 === null) {
return tree1 === tree2;
}
}

Step #3: The next step within our isMirror function, is adding to our conditional if statements by checking the value of the node from both tree1 and tree2. If the values of the nodes are not equal to each other, then we will return false otherwise we move to our next step.

// check the values of the nodes. if they are not the same, we can return false. otherwise, skip this if statement
if (tree1.val !== tree2.val) {
return false;
}

Step #4: When there is a value of neither null or the values are not equal to each other we want to return our isMirror function in order to complete our recursion calls for testing if the children nodes are mirror images. The left node of the right child should be the as the right node of the left child. Vice versa, the left node of the left child should then be the same as the right node of the right child. As we see in the example, both left and right nodes are mirror images.

// since we got past the first two if statements, make two recursive calls to test if the children are mirror images
// the left node of the right child should be the same as the right node of the left child
// likewise, the left node of the left child should be the same as the right node of the right child
return isMirror(tree1.left, tree2.right) && isMirror(tree1.right, tree2.left);
};

Step #5: Last but not least, we return back to our isSymmetric function and setup the return of passing in the left and right nodes within the isMirror function.

const isSymmetric = (root) => {
if (root === null) {
return true;
}

// pass in the left and right child nodes
return isMirror(root.left, root.right);
};

Thanks to the walkthrough provided by Terrible Whiteboard, I have a much better understanding of tackling recursion algorithm problems and I look forward to utilizing a similar approach to solve other recursion problems.

Another take away I learned was to use a whiteboard or write down the array and illustrate the changes that need to be made in order to solve the problem. This helps when writing out the pseudo code and thus the actual code.

Flatiron School Graduate with finance, budget, and tax experience.