Version 1 暴力 空间复杂度太高
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: Given an integer array with no duplicates.
* @return: The root of max tree.
*/
public TreeNode maxTree(int[] A) {
// 2015-09-05 暴力
if (A == null || A.length == 0) {
return null;
}
int index = findMax(A);
TreeNode root = new TreeNode(A[index]);
if (0 < index) {
int[] left = Arrays.copyOfRange(A, 0, index);
root.left = maxTree(left);
} else {
root.left = null;
}
if (index < A.length - 1) {
int[] right = Arrays.copyOfRange(A, index + 1, A.length);
root.right = maxTree(right);
} else {
root.right = null;
}
return root;
}
/**
* 返回序号
*/
private int findMax(int[] A) {
if (A == null || A.length == 0) {
return -1;
}
int index = -1;
int max = Integer.MIN_VALUE;
for (int i = 0; i < A.length; i++) {
if (max < A[i]) {
index = i;
max = A[i];
}
}
return index;
}
}
Version 2 暴力 StackOverflowError 题目给的数组太大
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: Given an integer array with no duplicates.
* @return: The root of max tree.
*/
public TreeNode maxTree(int[] A) {
// 2015-09-05 暴力
if (A == null || A.length == 0) {
return null;
}
return helper(A, 0, A.length - 1);
}
/**
* 改进,降低空间复杂度
*/
private TreeNode helper(int[] A, int start, int end) {
if (start > end) {
return null;
}
int index = findMax(A, start, end);
TreeNode root = new TreeNode(A[index]);
root.left = helper(A, start, index - 1);
root.right = helper(A, index + 1, end);
return root;
}
/**
* 返回序号
*/
private int findMax(int[] A, int start, int end) {
if (start > end) {
return -1;
}
int index = -1;
int max = Integer.MIN_VALUE;
for (int i = start; i <= end; i++) {
if (max < A[i]) {
index = i;
max = A[i];
}
}
return index;
}
}
Version 3
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: Given an integer array with no duplicates.
* @return: The root of max tree.
*/
public TreeNode maxTree(int[] A) {
// 2015-09-05
if (A == null || A.length == 0) {
return null;
}
// 栈中点的值递减
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
// 循环len + 1次
for (int i = 0; i <= A.length; i++) {
// 假设在数组的最后加上第n+1个数,大小为正无穷
TreeNode curNode = (i == A.length) ? new TreeNode(Integer.MAX_VALUE) : new TreeNode(A[i]);
// 一次循环pop一次
while (!stack.isEmpty() && curNode.val > stack.peek().val) {
TreeNode popNode = stack.pop();
// 下面分析将popNode放在哪
if (stack.isEmpty()) {
curNode.left = popNode;
} else {
TreeNode leftPop = stack.peek();
if (leftPop.val > curNode.val) {
curNode.left = popNode;
} else {
leftPop.right = popNode;
}
}
} // while
stack.push(curNode);
}
// 最后push进去的是第n + 1个点
return stack.peek().left;
}
}