刷题leetcode--654. Maximum Binary Tree

12 篇文章 0 订阅
11 篇文章 0 订阅

2018.1.8 15:47Fighting!

Leetcode.654. Maximum Binary Tree

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

  1. The root is the maximum number in the array.
  2. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  3. 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:

  1. The size of the given array will be in the range [1,1000].
题意:给一个数组,按照数组里面的最大值把数组拆成左右两个树,子树也是按照分割后的数组进行同样的划分,最后返回成二叉树。
思路:类似与给前序遍历,和中序遍历,让你重建二叉树。见之前博客:重建二叉树
就是找到最大值后根据这个位置分割数组为左右两个,每个再单独递归调用继续找到最大值分割。
(之前的前序遍历是根据根节点在中序遍历中找到根节点后,分割出左右子树,再递归在子树里面找根节点再分割)
需要写辅助函数,函数参数是(该数组,数组的左边界,数组右边界),每次迭代进去时缩小上下边界的范围。
(2)边界条件:数组为空的时候就返回空,非空调用辅助函数。
        辅助函数的边界条件:左边界大于右边。 
总结:凡是返回是二叉树形式的,递归的写法都是
    TreeNode root= new TreeNode(val);
    root.left=funcName(root.left, nums[ ] ,或者上下边界缩小)
    root.right = funcName(root.right, nums[ ] ,或者上下边界缩小 )

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        if(nums==null) return null;
        return buildTree(nums,0,nums.length-1);
              
    }
    
    public TreeNode buildTree(int[] nums,int left, int right){
        if(left>right) return null;
        int max=left;
        for(int i=left;i<=right;i++){
            if(nums[i]>nums[max]){
                max=i;
            }
        }
        TreeNode root = new TreeNode(nums[max]);
        root.left = buildTree(nums,left,max-1);
        root.right = buildTree(nums,max+1,right);
        return root;
    }
}
另一种思路是使用 Array.copyOfRange(int[] nums ,int  from, int to),复制出子数组,把子数组带入到递归函数里,但是这样会占用空间,可以直接在原数组上写上下届的索引值。   Array接口方法
            int[] subleft = Arrays.copyOfRange(nums,0,t);
            int[] subright = Arrays.copyOfRange(nums,t+1,nums.length);
            root.left = constructMaximumBinaryTree(subleft);
            root.right = constructMaximumBinaryTree(subright);
public static int[] copyOfRange(int[] original, int from, int to)                      

将指定数组的指定范围复制到一个新数组。该范围的初始索引 (from) 必须位于 0 和
original.length(包括)之间。original[from] 处的值放入副本的初始元素中(除非 from == original.length 或 from == to)。原数组中后续元素的值放入副本的后续元素。该范围的最后索引 (to)(必须大于等于 from)可以大于 original.length,在这种情况下,0 被放入索引大于等于 original.length - from 的副本的所有元素中。返回数组的长度为 to - from。

参数:

original - 将要从其复制一个范围的数组

from - 要复制的范围的初始索引(包括

to - 要复制的范围的最后索引(不包括)。(此索引可以位于数组范围之外)。

返回:

包含取自原数组指定范围的新数组,截取或用 0 填充以获得所需长度.

copyOfRange是输入java.util包中的Arrays类的静态内部方法,可以被类直接调用。下面以int[]型传递参数为例,来测试其用法。

copyOfRange(int []original,int from,int to),original为原始的int型数组,from为开始角标值,to为终止角标值。(其中包括from角标,不包括to角标。即处于[from,to)状态)下面是它的一个测试用法:


这个方法比循环遍历复制数组效率要高。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值