Given an integer array with no duplicates. A Max Tree Building on the this array is defined as follow:
- The root is the maximum number in the array
- The left subtree and subtree is the max trees of the Subarray divided by the root number.
Construct the Max tree by the given array.
Example
Given [2, 5, 6, 0, 3, 1], the max tree is
6
/ \
5 3
/ / \
2 0 1
Challenge
O (n) Time complexity
Analysis:
Recursion:use recursion method, in the worst case, the complexity is O (n^2).
Linear Method:refer to the analysis of some other people:http://www.meetqun.com/thread-3335-1-1.html
This question Leetcode not, in fact this kind of tree is called the flute Kasha (Cartesian tree). The complexity of the direct recursive contribution will degrade to O (n^2). The classic method of achievement is to use a monotonous stack. The tree stored in our stack has only left dial hand trees, there are no subtrees, and the root node is the largest.
(1) If a new number is smaller than the number of roots on the top of the stack, the number is pressed into the stack as a separate node.
(2) Otherwise, constantly pop the tree from the stack, the new pop-up tree with the old pop-up tree as the right subtree, connected, until the current stack top of the number of roots is greater than the number of new. Then, the number that pops up has formed a new tree, which acts as the left subtree of the new node, pressing the new tree onto the stack.
Such stacks are monotonous, and the closer to the top of the stack the smaller the number .
Finally, according to the method (2), all the trees are bounced out, each old tree as the right subtree of the new tree.
Solution:
1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9 * }Ten * } One */ A Public classSolution { - /** - * @parama:given An integer array with no duplicates. the * @return: The root of Max tree. - */ - PublicTreeNode Maxtree (int[] A) { - if(a.length==0)return NULL; + -Stack<treenode> Nodestack =NewStack<treenode>(); +Nodestack.push (NewTreeNode (a[0])); A for(inti=1;i<a.length;i++) at if(a[i]<=Nodestack.peek (). val) { -TreeNode node =NewTreeNode (A[i]); - Nodestack.push (node); -}Else { -TreeNode N1 =Nodestack.pop (); - while(!nodestack.isempty () && Nodestack.peek (). val <A[i]) { inTreeNode N2 =Nodestack.pop (); -N2.right =N1; toN1 =N2; + } -TreeNode node =NewTreeNode (A[i]); theNode.left =N1; * Nodestack.push (node); $ }Panax Notoginseng - theTreeNode root =Nodestack.pop (); + while(!Nodestack.isempty ()) { ANodestack.peek (). Right =Root; theRoot =Nodestack.pop (); + } - $ returnRoot; $ - - the - Wuyi } the}
Solution 2 (recursion):
1 /**2 * Definition of TreeNode:3 * public class TreeNode {4 * public int val;5 * Public TreeNode left and right;6 * Public TreeNode (int val) {7 * This.val = val;8 * This.left = This.right = null;9 * }Ten * } One */ A Public classSolution { - /** - * @parama:given An integer array with no duplicates. the * @return: The root of Max tree. - */ - PublicTreeNode Maxtree (int[] A) { - if(a.length==0)return NULL; + -TreeNode root =NewTreeNode (a[0]); + for(inti=1;i<a.length;i++) A if(a[i]>root.val) { atTreeNode node =NewTreeNode (A[i]); -Node.left =Root; -Root =node; -}ElseInsertnode (Root,NULL, A[i]); - - returnRoot; in } - to Public voidInsertnode (TreeNode cur, TreeNode Pre,intval) { + if(cur==NULL){ -TreeNode node =NewTreeNode (val); thePre.right =node; * return; $ }Panax Notoginseng - if(cur.val<val) { theTreeNode node =NewTreeNode (val); +Pre.right =node; ANode.left =cur; the return; +}Else - Insertnode (cur.right,cur,val); $ } $ - -}
Lintcode-max Tree