// calculate the size of binary tree (number of nodes)
const sizeTree = (root) => {
if (!root) return 0;
let queue = [root];
let size = 0;
while (queue.length > 0) {
let current = queue.shift();
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
size += 1;
}
return size;
};
// calculate the maximum height of binary tree
const maxHeightTree = (root) => {
if (!root) return 0;
let left = maxHeightTree(root.left);
console.log(left)
let right = maxHeightTree(root.right);
console.log(right)
if(left> right){
return left +1 ;
}else {
return right+1 ;
}
};
inOrder() {
let result = [];
let recursive = (node) => {
if (node.left) {
recursive(node.left);
}
result.push(node.value);
if (node.right) {
recursive(node.right);
}
}
recursive(this.root);
return result;
}
//if you find the answer is useful ,
//upvote for it, so can the others benefit also ༼ つ ◕_◕ ༽つ @mohammad alshraideh ( ͡~ ͜ʖ ͡°)⇑⇑
//print Kth path (given K integer and you need to find the path to reach this K as total sum in binary tree)
const printKthPath =(root ) =>{
let k =7 ;
if(root === null) return
let sum = 0 ;
let stack =[] ;
sum =sum +root.data ;
stack.push(root.data)
if(sum === k){
return stack
}else {
if(root.left)printKthPath(root.left )
if(root.right)printKthPath(root.right)
if(!root.right && ! root.left ){
sum = sum- root.data;
stack.pop();
}
}
return stack
}
// calculate the average value for binary tree
const AvgValue = (root) => {
let result = [];
let avg = 0;
let sum = 0;
let queue = [root];
while (queue.length) {
let current = queue.shift();
result.push(current.data);
if (current.left) queue.push(current.left);
if (current.right) queue.push(current.right);
}
for (let i = 0; i < result.length; i++) {
sum = sum + result[i];
}
avg = Math.floor(sum / result.length);
return avg;
};
// minimum node in binary tree
const 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) return true ;
if(tree1.data == tree2.data){
if(isMirror(tree1.left ,tree2.right) && isMirror(tree1.right ,tree2.left)){
return true
}else return false
} else{
return false
}
}
// invert binary tree using recursive function
function invertTree(node) {
if (node && node.left) {
let left = node.left;
node.left = node.right;
node.right = left;
invertTree(node.right);
invertTree(node.left);
}
return node;
}
//claculate the minimum height of binary tree
const minHeightTree =(root)=>{
if (!root) return 0;
let left = minHeightTree(root.left);
console.log(left)
let right = minHeightTree(root.right);
console.log(right)
if(left< right){
return left +1 ;
}else {
return right+1 ;
}
};