Invert Binary Tree With JavaScript
Recursive Algorithm To Invert A Binary Tree

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...
Recursive Algorithm To Invert A Binary Tree

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, I will be showing you how to solve the LeetCode Invert Binary Tree problem using a recursive approach. I'll explain each step of developing a solution to help you improve your problem solving skills.
Lets get right into it

The question is asking us to invert a binary tree. Pretty simple right?
The structure of a binary tree node from LeetCode is as follows:
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
So the function takes in one parameter - root, and then returns the same root with the rest of the tree reversed.
We will be using recursion to create a solution since I believe its the simplest approach. Within our base function LeetCode automatically generates:
var invertTree = function(root) {
};
We will create a function called reverseNode which will reverse a node's left and right nodes. This function will take in one parameter - node:
var invertTree = function(root) {
const reverseNode = node => {
}
};
The first part of function will be checking if the node is null. This would mean that there is either no root or there is no further part of the tree. If the node is null, we will return null from the function:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
}
};
This is essentially our way of ending the recursion so it doesn't run forever.
After that conditional statement, we will recursively call the reverseNode function passing in both node.left and node.right so it reverses both sides of the tree.
Adding that to our code looks like this:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
}
};
After that, we obviously have to actually reverse the node which means we will need to swap the left and right values. To do this, we will need a variable that stores node.left so we can hold the original value after changing it to node.right.
Lets call this variable holdLeft:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
let holdLeft = node.left;
}
};
I'm using
letto define the variable, but you could useconstas well.
After declaringholdLeft, we can swap the left and right nodes by reassigning node.left to node.right and reassigning node.right to the holdLeft value:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
let holdLeft = node.left;
node.left = node.right;
node.right = holdLeft;
}
};
We then want to return node from the function:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
let holdLeft = node.left;
node.left = node.right;
node.right = holdLeft;
return node;
}
};
Finally, to complete our solution, we will need to return reverseNode(root) from the invertTree function so that it reverses the tree passed into the function.
Our completed solution now looks like this:
var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
let holdLeft = node.left;
node.left = node.right;
node.right = holdLeft;
return node;
}
return reverseNode(root);
};
You've now learned how to invert a binary tree using the JavaScript programming language. Its a fairly simple, elegant solution that uses recursion to effectively invert the tree.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake-K-Yeboah
You can also show your support by buying me a coffee ๐