
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Inverting a binary tree in JavaScript
Suppose, we have a binary tree represented like this −
4 / \ 2 7 / \ / \ 1 3 6 9
We are required to write a JavaScript function that takes in the root of this binary tree and inverts it.
The inverted version of this above binary tree will look like this −
4 / \ 7 2 / \ / \ 9 6 3 1
Example
The code for this will be −
// class for a single tree node class Node{ constructor(val){ this.val = val; this.left = null; this.right = null; }; }; // class for binary tree class BinaryTree{ constructor(){ // root of the binary tree this.root = null; }; insert = (data) => { // creating a new node with data const newNode = new Node(data); // if root is null, then this node will be the root if(this.root === null){ this.root = newNode; }else{ // otherwise we find the correct position to insert this node this.insertData(this.root, newNode); }; }; insertData = (node, newNode) => { if(newNode.val < node.val){ if(node.left === null){ node.left = newNode; }else{ this.insertData(node.left, newNode); } }else{ if(node.right === null){ node.right = newNode; }else{ this.insertData(node.right, newNode); } } }; // function to invert the binary tree invert = (node) => { if(node === null){ return; }; [node.left, node.right] = [node.right, node.left]; this.invert(node.right); this.invert(node.left); } traverse = (node) => { if(node === null){ return; }; this.traverse(node.right); console.log(node.val); this.traverse(node.left); }; }; const Tree = new BinaryTree(); Tree.insert(2); Tree.insert(7); Tree.insert(4); Tree.insert(1); Tree.insert(9); Tree.insert(3); Tree.insert(6); // original right to left traversal Tree.traverse(Tree.root); Tree.invert(Tree.root); console.log('after inversion'); // traversal right to left after inversion Tree.traverse(Tree.root);
Output
And the output in the console will be −
9 7 6 4 3 2 1 after inversion 1 2 3 4 6 7 9
- Related Articles
- Binary Tree in Javascript
- Implementing a Binary Search Tree in JavaScript
- Binary Search Tree in Javascript
- Inverting slashes in a string in JavaScript
- Binary Search Tree Class in Javascript
- Inverting signs in array - JavaScript
- Finding Mode in a Binary Search Tree in JavaScript
- Checking for univalued Binary Search Tree in JavaScript
- Binary Tree to Binary Search Tree Conversion in C++
- Finding minimum absolute difference within a Binary Search Tree in JavaScript
- Check if a binary tree is subtree of another binary tree in C++
- Searching for values in an Javascript Binary Search Tree
- Difference between Binary Tree and Binary Search Tree
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Inverting and Non-Inverting Operational Amplifiers

Advertisements