Define the binary tree nodes as follows:
public class Node {public int value; Public Node left; public Node right; public Node (int data) { this.value = data; } }
The maxtree of an array is defined as follows.
The array must have no duplicate elements.
Maxtree is a binary tree, and each value of the array corresponds to a binary tree node.
Including the Maxtree tree and on each of the subtrees trees, the node with the largest value is the head of the tree.
Given an array of non-repeating elements, arr writes out a function that generates the maxtree of this array, requiring that if the array length is n, the time complexity is O (n) and the extra Space complexity is O (n).
Solution One: can use the heap, the time complexity of constructing the heap is O (N)
Solution Two: The use of a monotone stack for each position in the array of the most recent value, and then let the left and right are null nodes as the head node, only the left side of the big left as the left side of the child, only the right side of the node as large as the right side of the child, about the larger than it has, choose two smaller Children as smaller nodes
Data structure--monotone stack--maxtree of constructing arrays