Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Merge Two Binary Trees

// Depth first search approach
// We will use t1 as the merged tree
var mergeTrees = function(t1, t2) {
    // If any of the nodes is null then use the other node as the merged node
    if (t1 === null)
        return t2;
    if (t2 === null)
        return t1;
    // Add the vals of current node for each step if they are not null
    t1.val += t2.val;
    // Explore remaining children in the trees
    t1.left = mergeTrees(t1.left, t2.left);
    t1.right = mergeTrees(t1.right, t2.right);
    return t1;
};
Comment

merge two binary tree

// merge two binary tree using recursive function 
function mergeTwoBT(t1,t2){

  if(t1==null)return t2;
  if(t2==null)return t1;
  t1.data= t1.data + t2.data
  t1.left =mergeTwoBT(t1.left,t2.left);
  t1.right =mergeTwoBT(t1.right,t2.right);
  return t1 ;

  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs beautifulsoup 
Javascript :: prototype javascript 
Javascript :: sintax arrow function in javascript 
Javascript :: pwa clear cache 
Javascript :: react validation form 
Javascript :: trigger mouseover on element devtools 
Javascript :: datepicker date and time 
Javascript :: setattribute 
Javascript :: how fetch multiple data in javascript react 
Javascript :: how to draw a long underline in react native 
Javascript :: check a letter in astring js 
Javascript :: google script new date 
Javascript :: javascript create string of given length 
Javascript :: js how to filter range in place 
Javascript :: angular reference element 
Javascript :: jquery check valid link 
Javascript :: javascript Symbol Methods 
Javascript :: method example js 
Javascript :: javascript array with random values 
Javascript :: js remove all attributes from element 
Javascript :: how to do something once in javascript 
Javascript :: react array if id is present do not add element 
Javascript :: error handling in node.js 
Javascript :: setinterval on and off 
Javascript :: how i get selected class of li in jquery 
Javascript :: do i need to know javascript to learn three.js 
Javascript :: js get elements in array from x to y 
Javascript :: debug bar laravel unninstall 
Javascript :: window parent frames javascript 
Javascript :: npx vs npm 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =