Data structure--monotone stack--maxtree of constructing arrays

Source: Internet
Author: User
Tags array length

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.