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.
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
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:
20 2 2 5 8 8
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Construct Special Binary Tree from given Inorder traversal
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct Binary Tree from given Parent Array representation
- Construct a Binary Search Tree from given postorder
- Construct Ancestor Matrix from a Given Binary Tree
- Construct a complete binary tree from given array in level order fashion
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Count the Number of Binary Search Trees present in a Binary Tree
- Construct Complete Binary Tree from its Linked List Representation
- Construct a Binary Tree from Postorder and Inorder
- Construct Binary Tree from String with bracket representation
- Construct Binary Tree from Ancestor Matrix | Top Down Approach
- Construct Tree from given Inorder and Preorder traversals
- Construct a special tree from given preorder traversal
- Maximum cost of splitting given Binary Tree into two halves
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Amal Kumar Choubey