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 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 :: is loop backward 
Javascript :: react router v6 
Javascript :: add style by classname javascript 
Javascript :: js new array 
Javascript :: react form validation 
Javascript :: Generate a random Id safely 
Javascript :: nodemon writefilesync restart problem 
Javascript :: JavaScript Extract Values 
Javascript :: Find Dubplicate In Array of Object 
Javascript :: javascript eventlistener 
Javascript :: mobile detect js 
Javascript :: react to pdf 
Javascript :: nan in js 
Javascript :: nodejs mysql transactions 
Javascript :: javascript callbacks anonymous function 
Javascript :: javascript add field to array 
Javascript :: a full express function 
Javascript :: mongoose node js 
Javascript :: get element attribute jquery 
Javascript :: javascript strings are immutable 
Javascript :: get dynamic value in jquery 
Javascript :: find number in array js 
Javascript :: javascript regex insert string 
Javascript :: $(...).DataTable is not a function 
Javascript :: how to run a react app 
Javascript :: string immutable javascript 
Javascript :: knex pagination plugin 
Javascript :: how to add css based on route react 
Javascript :: window parent frames js 
Javascript :: how to open cypress 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =