[LeetCode] 654. Maximum Binary Tree

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

The root is the maximum number in the array.
The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
Construct the maximum tree by the given array and output the root node of this tree.

Example 1:

Input: [3,2,1,6,0,5]
Output: return the tree root node representing the following tree:

     6
   /   
  3     5
       / 
    2  0   
      
       1

Note:
The size of the given array will be in the range [1,1000].


题目不算难,可以很容易的想到解决思路:

  1. 创建一个根节点,其值为数组的最大值
  2. 创建根节点的左节点,左节点的值为数组左半部分的最大值
  3. 创建根节点的右节点,右节点的值为数组右半部分的最大值
  4. 递归以上操作

于是我用最直接的代码写出了以下解决方法

Solution:

   class TreeNode {
   	int val;
   	TreeNode left;
   	TreeNode right;
   	TreeNode(int x) {
   		val = x;
   	}
   }
   public class Solution {
   	public static TreeNode constructMaximumBinaryTree(int[] nums) {
   		if(nums.length==0) return null;
   		int max=getMaxNumber(nums);
   		TreeNode root=new TreeNode(max);
   		root.left=constructMaximumBinaryTree(getArrayLeftPart(max, nums));
   		root.right=constructMaximumBinaryTree(getArrayRightPart(max, nums));
   		return root;
   	}
   	public static int getMaxNumber(int[] nums) {
   		int[] tempArray = nums.clone();
   		Arrays.sort(tempArray);
   		return tempArray[tempArray.length - 1];
   	}
   	public static int[] getArrayLeftPart(int spilt, int[] nums) {
   		List<Integer> list = new ArrayList<Integer>();
   		for (int i = 0; i < nums.length; i++) {
   			if (nums[i] == spilt) {
   				break;
   			}
   			list.add(nums[i]);
   		}
   		int[] result = new int[list.size()];
   		for (int k = 0; k < list.size(); k++) {
   			result[k] = list.get(k);
   		}
   		return result;
   	}
   	public static int[] getArrayRightPart(int spilt, int[] nums) {
   		List<Integer> list = new ArrayList<Integer>();
   		for (int i = nums.length - 1; i > 0; i--) {
   			if (nums[i] == spilt) {
   				break;
   			}
   			list.add(nums[i]);
   		}
   		int[] result = new int[list.size()];
   		for (int k = list.size() - 1; k >= 0; k--) {
   			result[list.size() - k - 1] = list.get(k);
   		}
   		return result;
   	}
   }

这个解法给了AC,但是效率很低,Leetcode给出的运行时间是433ms。对于性能差我能想到的原因有:

  1. 频繁的创建数组,获取数组最大值需要克隆数组,获取数组的左半部分也需要从list转为数组,这些都会影响程序的性能。
  2. 频繁遍历数组,这个不用我多说,看代码就可以。

性能优化

上面的方法使用在递归时传的是值(value),这样的递归需要传递的参数个数比较少,但对资源的利用率不高。所以我们改为传递索引(index)的方式,提高对数组的利用率。这样在数据传递的时候只需要传递数组的索引值,整个递归过程只需要对一个数组进行操作。

    public static TreeNode constructMaximumBinaryTree(int[] nums){
   		 return constructor(nums,0,nums.length-1);
   	 }
   	 //给出的method只能传递一个参数,所以用一个新的method
   	 public static TreeNode constructor(int[] nums,int left,int right){
   		 if (left>right)
   			 return null;
   		 int max=MaxArrayIndex(nums, left, right);
   		 TreeNode root=new TreeNode(nums[max]);
   		 root.left=constructor(nums,left,max-1);
   		 root.right=constructor(nums, max+1, right);
   		 return root;
   	 }
   	 //获取数组中最大值的索引
   	 public static int MaxArrayIndex(int[] nums,int left,int right){
   		 int point=nums[left];
   		 int max_index=left;
   		 for(int i=left+1;i<=right;i++){
   			 if(nums[i]>point){
   				 point=nums[i];
   				 max_index=i;
   			 }else continue;
   		 }
   		 return max_index;
   	 }

改进后运行时间降到了13ms,超过了66.8%的提交。

边界问题

边界问题似乎不是什么大问题,但它却会在一定程度上阻碍你解题的速度。

首先我们得先了解哪里会出现数组越界的情况。容易想到越界肯定容易在索引发生变化的地方出现,也就是在递归那一步的时候。由于在递归时我们改变了数组的索引,所以我们必须在函数的第一步判断索引是不是有效的。在本例中你需要保证:

  1. MaxArrayIndex 函数的索引保持的合法范围内。
  2. constructMaximumBinaryTree 的参数保持在合法范围内,本例中显然没有问题。
  3. 递归函数constructor 中合理的索引校验

第一点保证了递归内部获取的索引一定是合法的。第二点保证了,调用递归时索引的值是合法的。第三点保证了子递归出现索引问题时能够及时阻断这个子递归。

原文地址:https://www.cnblogs.com/rever/p/8004106.html