Construct a Maximum Binary Tree from two given Binary Trees
Last Updated :
22 Jun, 2021
Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree.
What is the maximum Binary Tree?
The maximum binary is constructed in the following manner:
In the case of both the Binary Trees having two corresponding nodes, the maximum of the two values is considered as the node value of the Maximum Binary Tree.
If any of the two nodes is NULL and if the other node is not null, insert that value on that node of the Maximum Binary Tree.
Example:
Input:
Tree 1 Tree 2
3 5
/ \ / \
2 6 1 8
/ \ \
20 2 8
Output: 20 2 2 5 8 8
Explanation:
5
/ \
2 8
/ \ \
20 2 8
To construct the required Binary Tree,
Root Node value: Max(3, 5) = 5
Root->left value: Max(2, 1) = 2
Root->right value: Max(6, 8) = 8
Root->left->left value: 20
Root->left->right value: 2
Root->right->right value: 8
Input:
Tree 1 Tree 2
9 5
/ \ / \
2 6 1 8
/ \ \ \
20 3 2 8
Output: 20 2 3 9 8 8
Explanation:
9
/ \
2 8
/ \ \
20 3 8
Approach:
Follow the steps given below to solve the problem:
- Traverse both the trees using preorder traversal.
- If both the nodes are NULL, return. Otherwise, check for the following conditions:
- If both the nodes are not NULL then store the maximum between them as the node value of the Maximum Binary Tree.
- If only one of the node is NULL store the value of the non-NULL node as the node value of the Maximum Binary Tree.
- Recursively traverse the left subtrees.
- Recursively traverse the right subtrees
- Finally, return the root of the Maximum Binary Tree.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
class Node{
public :
int data;
Node *left, *right;
Node( int data, Node *left,
Node *right)
{
this ->data = data;
this ->left = left;
this ->right = right;
}
};
Node* newNode( int data)
{
Node *tmp = new Node(data, NULL, NULL);
return tmp;
}
void inorder(Node *node)
{
if (node == NULL)
return ;
inorder(node->left);
cout << node->data << " " ;
inorder(node->right);
}
Node* MaximumBinaryTree(Node *t1, Node *t2)
{
if (t1 == NULL)
return t2;
if (t2 == NULL)
return t1;
t1->data = max(t1->data, t2->data);
t1->left = MaximumBinaryTree(t1->left,
t2->left);
t1->right = MaximumBinaryTree(t1->right,
t2->right);
return t1;
}
int main()
{
Node *root1 = newNode(3);
root1->left = newNode(2);
root1->right = newNode(6);
root1->left->left = newNode(20);
Node *root2 = newNode(5);
root2->left = newNode(1);
root2->right = newNode(8);
root2->left->right = newNode(2);
root2->right->right = newNode(8);
Node *root3 = MaximumBinaryTree(root1, root2);
inorder(root3);
}
|
Java
class Node {
int data;
Node left, right;
public Node( int data, Node left,
Node right)
{
this .data = data;
this .left = left;
this .right = right;
}
static Node newNode( int data)
{
return new Node(data, null , null );
}
static void inorder(Node node)
{
if (node == null )
return ;
inorder(node.left);
System.out.printf( "%d " , node.data);
inorder(node.right);
}
static Node MaximumBinaryTree(Node t1, Node 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;
}
public static void main(String[] args)
{
Node root1 = newNode( 3 );
root1.left = newNode( 2 );
root1.right = newNode( 6 );
root1.left.left = newNode( 20 );
Node root2 = newNode( 5 );
root2.left = newNode( 1 );
root2.right = newNode( 8 );
root2.left.right = newNode( 2 );
root2.right.right = newNode( 8 );
Node root3
= MaximumBinaryTree(root1, root2);
inorder(root3);
}
}
|
Python3
class Node:
def __init__( self , data, left, right):
self .data = data
self .left = left
self .right = right
def newNode(data):
return Node(data, None , None );
def inorder(node):
if (node = = None ):
return ;
inorder(node.left);
print (node.data, end = ' ' );
inorder(node.right);
def MaximumBinaryTree(t1, t2):
if (t1 = = None ):
return t2;
if (t2 = = None ):
return t1;
t1.data = max (t1.data, t2.data);
t1.left = MaximumBinaryTree(t1.left,
t2.left);
t1.right = MaximumBinaryTree(t1.right,
t2.right);
return t1;
if __name__ = = '__main__' :
root1 = newNode( 3 );
root1.left = newNode( 2 );
root1.right = newNode( 6 );
root1.left.left = newNode( 20 );
root2 = newNode( 5 );
root2.left = newNode( 1 );
root2.right = newNode( 8 );
root2.left.right = newNode( 2 );
root2.right.right = newNode( 8 );
root3 = MaximumBinaryTree(root1, root2);
inorder(root3);
|
C#
using System;
class Node{
public int data;
public Node left, right;
public Node( int data, Node left,
Node right)
{
this .data = data;
this .left = left;
this .right = right;
}
static Node newNode( int data)
{
return new Node(data, null , null );
}
static void inorder(Node node)
{
if (node == null )
return ;
inorder(node.left);
Console.Write( "{0} " , node.data);
inorder(node.right);
}
static Node MaximumBinaryTree(Node t1, Node 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;
}
public static void Main(String[] args)
{
Node root1 = newNode(3);
root1.left = newNode(2);
root1.right = newNode(6);
root1.left.left = newNode(20);
Node root2 = newNode(5);
root2.left = newNode(1);
root2.right = newNode(8);
root2.left.right = newNode(2);
root2.right.right = newNode(8);
Node root3 = MaximumBinaryTree(root1, root2);
inorder(root3);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...