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 :: reactjs npm take photo 
Javascript :: invert linked list js 
Javascript :: mern heroku Error: ENOENT: no such file or directory 
Javascript :: javascript countdown timer including days 
Javascript :: nodejs convert buffer to uint8array 
Javascript :: how to hide a screen from drawer in react navigation 5 
Javascript :: how to build with a specific .env file node 
Javascript :: chai async test 
Javascript :: using dot prototype with constructor in javascript 
Javascript :: how to Write a program that simulates a coin toss using random method of Javascript Math class 
Javascript :: how to use of socket io on a route in nodejs 
Javascript :: extjs clone object 
Javascript :: filter the falsy values out of an array in a very simple way! 
Javascript :: how to filter an array of strings to see which letters match javascript 
Javascript :: delete item from a foreach vuejs 
Javascript :: javascript max characters string function 
Javascript :: python run javascript 
Javascript :: grapesjs cdn 
Javascript :: check if alpine js is loaded 
Javascript :: svg event listeners 
Javascript :: split text javascript 
Javascript :: loop through elements by name js 
Javascript :: math.random 
Javascript :: json regex 
Javascript :: using javascript array create bootstrap card 
Javascript :: mongoose + populate 
Javascript :: Hide ReactTooltip after hover off 
Javascript :: alert by code stackoverflow 
Javascript :: vuejs nested v-for 
Javascript :: SHOPIFY COUNTRY SELECTOR 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =