Construct a Maximum Binary Tree from two given Binary Trees | A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to find the maximum binary tree from two binary trees ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8
class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } function newNode ( data ) { return new Node ( data , null , null ) ; } function inorder ( node ) { if ( node == null ) return ; inorder ( node . left ) ; document . write ( node . data + " " ) ; inorder ( node . right ) ; } function MaximumBinaryTree ( t1 , t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . data = Math . max ( t1 . data , t2 . data ) ; t1 . left = MaximumBinaryTree ( t1 . left , t2 . left ) ; t1 . right = MaximumBinaryTree ( t1 . right , t2 . right ) ; return t1 ; } var root1 = newNode ( 3 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 6 ) ; root1 . left . left = newNode ( 20 ) ; var root2 = newNode ( 5 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 8 ) ; root2 . left . right = newNode ( 2 ) ; root2 . right . right = newNode ( 8 ) ; var root3 = MaximumBinaryTree ( root1 , root2 ) ; inorder ( root3 ) ;