654. Maximum Binary Tree LeetCode Solution

In this guide, you will get 654. Maximum Binary Tree LeetCode Solution with the best time and space complexity. The solution to Maximum Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Binary Tree solution in C++
  4. Maximum Binary Tree solution in Java
  5. Maximum Binary Tree solution in Python
  6. Additional Resources
654. Maximum Binary Tree LeetCode Solution image

Problem Statement of Maximum Binary Tree

You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:

Create a root node whose value is the maximum value in nums.
Recursively build the left subtree on the subarray prefix to the left of the maximum value.
Recursively build the right subtree on the subarray suffix to the right of the maximum value.

Return the maximum binary tree built from nums.

Example 1:

Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
– The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
– The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
– Empty array, so no child.
– The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
– Empty array, so no child.
– Only one element, so child is a node with value 1.
– The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
– Only one element, so child is a node with value 0.
– Empty array, so no child.

See also  860. Lemonade Change LeetCode Solution

Example 2:

Input: nums = [3,2,1]
Output: [3,null,2,null,1]

Constraints:

1 <= nums.length <= 1000
0 <= nums[i] <= 1000
All integers in nums are unique.

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(h)

654. Maximum Binary Tree LeetCode Solution in C++

class Solution {
 public:
  TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
    return build(nums, 0, nums.size() - 1);
  }

 private:
  TreeNode* build(const vector<int>& nums, int i, int j) {
    if (i > j)
      return nullptr;

    const auto it = max_element(nums.begin() + i, nums.begin() + j + 1);
    const int maxNum = *it;
    const int maxIndex = it - nums.begin();

    TreeNode* root = new TreeNode(maxNum);
    root->left = build(nums, i, maxIndex - 1);
    root->right = build(nums, maxIndex + 1, j);
    return root;
  }
};
/* code provided by PROGIEZ */

654. Maximum Binary Tree LeetCode Solution in Java

class Solution {
  public TreeNode constructMaximumBinaryTree(int[] nums) {
    return build(nums, 0, nums.length - 1);
  }

  private TreeNode build(int[] nums, int i, int j) {
    if (i > j)
      return null;

    int maxIndex = i;
    for (int k = i + 1; k <= j; ++k)
      if (nums[k] > nums[maxIndex])
        maxIndex = k;

    TreeNode root = new TreeNode(nums[maxIndex]);
    root.left = build(nums, i, maxIndex - 1);
    root.right = build(nums, maxIndex + 1, j);
    return root;
  }
}
// code provided by PROGIEZ

654. Maximum Binary Tree LeetCode Solution in Python

class Solution:
  def constructMaximumBinaryTree(self, nums: list[int]) -> TreeNode | None:
    def build(i: int, j: int) -> TreeNode | None:
      if i > j:
        return None

      maxNum = max(nums[i:j + 1])
      maxIndex = nums.index(maxNum)

      root = TreeNode(maxNum)
      root.left = build(i, maxIndex - 1)
      root.right = build(maxIndex + 1, j)
      return root

    return build(0, len(nums) - 1)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.