
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Binary Tree in C++
Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −
The root will hold the maximum number in the array.
The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.
The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.
We have to construct the maximum binary tree. So if the input is like: [3,2,1,6,0,5], then the output will be −
To solve this, we will follow these steps −
Define a method called solve(), this will take the list and left and right values. The function will work as follows −
if left > right, then return null
maxIndex := left and maxVal := nums[left]
for i in range left + 1 to right
if maxVal < nums[i], then maxVal := nums[i], maxIndex := i
define a node with value maxVal
left of node := solve(nums, left, maxIndex - 1)
right of node := solve(nums, maxIndex + 1, right)
return node
The solve method will be called from the main section as: solve(nums, 0, length of nums array - 1)
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; } else { q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; } else { q.push(temp->right); } } } TreeNode *make_tree(vector<int> v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } void inord(TreeNode *root){ if(root != NULL){ inord(root->left); cout << root->val << " "; inord(root->right); } } class Solution { public: TreeNode* solve(vector <int>& nums, int left, int right){ if(left>right)return NULL; int maxIndex = left; int maxVal = nums[left]; for(int i = left + 1; i <= right; i++){ if(maxVal < nums[i]){ maxVal = nums[i]; maxIndex = i; } } TreeNode* node = new TreeNode(maxVal); node->left = solve(nums, left, maxIndex - 1); node->right = solve(nums, maxIndex + 1, right); return node; } TreeNode* constructMaximumBinaryTree(vector<int>& nums) { return solve(nums, 0, nums.size() - 1); } }; main(){ vector<int> v = {4,3,2,7,1,6}; Solution ob; inord(ob.constructMaximumBinaryTree(v)); }
Input
[3,2,1,6,0,5]
Output
4 3 2 7 1 6
- Related Articles
- Maximum Binary Tree II in C++
- Maximum Width of Binary Tree in C++
- Maximum Depth of Binary Tree in Python
- Binary Tree Maximum Path Sum in Python
- Maximum spiral sum in Binary Tree in C++
- Maximum Sum BST in Binary Tree in C++
- Maximum width of a binary tree in C++
- Maximum parent children sum in Binary tree in C++
- Find maximum level product in Binary Tree in C++
- Find maximum vertical sum in binary tree in C++
- Maximum Path Sum in a Binary Tree in C++
- Find maximum (or minimum) in Binary Tree in C++
- Find maximum level sum in Binary Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Maximum Consecutive Increasing Path Length in Binary Tree in C++
