// invert binary tree : (right node >> left node && left node >> right node )const invertTree =(root)=>{if(root===null)return root;
let queue =[root];while(queue.length>0){
let node =queue.shift();if(node !=null){
let hold = node.left ;
node.left =node.right;
node.right =hold ;if(node.left) queue.push(node.left);if(node.right) queue.push(node.right);}}return root;}
contains(value){
let current =this.root;while(current){if(value < current.value){
current = current.left;}elseif(value > current.value){
current = current.right;}else{returntrue;}}returnfalse;}}//if you find this answer is useful ,//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
Add(value){
let current =this.root;if(!current){this.root =newNode(value);}else{while(current){if(value < current.value){if(!current.left){
current.left =newNode(value);break;}
current = current.left;}else{if(!current.right){
current.right =newNode(value);break;}
current = current.right;}}}}//if you find this answer is useful ,//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
peek(){returnthis.elements[this.head];}//if you find this answer is useful ,//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
compare(tree1 ,tree2 ){
let x =tree1.count()
let y =tree2.count()if(x==y)returntrue;elsereturnfalse;}//if you find this answer is useful ,//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
// minimum node in binary treeconst BreadthFirstsearchMin =(root)=>{
let min = Infinity ;
let queue =[root];while(queue.length){
let current = queue.shift();if(current.data < min){
min=current.data;}if(current.left!== null) queue.push(current.left);if(current.right !==null) queue.push(current.right);}return min ;}
// find maixmum node in binary tree const Max =(root)=>{
let max =-Infinity;
let stack =[root];while(stack.length){
let current = stack.pop();if(current.data > max){
max = current.data;}if(current.left !== null) stack.push(current.left);if(current.right !== null) stack.push(current.right);}return max;};
// check if mirror binary tree or not (give two binary trees and you need to find if they are a mirror to each other )const isMirror =(tree1 ,tree2)=>{if(tree1 ===null && tree2 ===null)returntrue;if(tree1.data == tree2.data){if(isMirror(tree1.left ,tree2.right)&&isMirror(tree1.right ,tree2.left)){returntrue}elsereturnfalse}else{returnfalse}}
#include<bits/stdc++.h>usingnamespace std;classNode{public:int data;
Node* left;
Node* right;// Val is the key or the value that// has to be added to the data partNode(int val){
data = val;// Left and right child for node// will be initialized to null
left =NULL;
right =NULL;}};intmain(){/*create root*/
Node* root =newNode(1);/* following is the tree after above statement
1
/
NULL NULL
*/
root->left =newNode(2);
root->right =newNode(3);/* 2 and 3 become left and right children of 1
1
/
2 3
/ /
NULL NULL NULL NULL
*/
root->left->left =newNode(4);/* 4 becomes left child of 2
1
/
2 3
/ /
4 NULL NULL NULL
/
NULL NULL
*/return0;}