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.
- Count the Number of Binary Search Trees present in a Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Construct a Binary Tree from Postorder and Inorder
- Construct a Binary Search Tree from given postorder
- If you are given two traversal sequences, can you construct the binary tree?
- Construct Ancestor Matrix from a Given Binary Tree
- Construct Binary Tree from Ancestor Matrix | Top Down Approach
- Construct Binary Tree from given Parent Array representation
- Construct Binary Tree from String with bracket representation
- Construct Special Binary Tree from given Inorder traversal
- Construct Full Binary Tree from given preorder and postorder traversals
- Construct Complete Binary Tree from its Linked List Representation
- Construct a complete binary tree from given array in level order fashion
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum spiral sum in Binary Tree
- Maximum width of a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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