最大二叉树

给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:

  1. 二叉树的根是数组中的最大元素。
  2. 左子树是通过数组中最大值左边部分构造出的最大二叉树。
  3. 右子树是通过数组中最大值右边部分构造出的最大二叉树。

通过给定的数组构建最大二叉树,并且输出这个树的根节点。

Example 1:

输入: [3,2,1,6,0,5]

输入: 返回下面这棵树的根节点:

注意:

  1. 给定的数组的大小在 [1, 1000] 之间。

 public class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums, 0, nums.length);
}
public TreeNode construct(int[] nums, int l, int r) {
if (l == r)
return null;
int max_i = max(nums, l, r);
TreeNode root = new TreeNode(nums[max_i]);
root.left = construct(nums, l, max_i);
root.right = construct(nums, max_i + 1, r);
return root;
}
public int max(int[] nums, int l, int r) {
int max_i = l;
for (int i = l; i < r; i++) {
if (nums[max_i] < nums[i])
max_i = i;
}
return max_i;
}
}

Leetcode 654.最大二叉树的更多相关文章

  1. Java实现 LeetCode 654 最大二叉树(递归)

    654. 最大二叉树 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左子树是通过数组中最大值左边部分构造出的最大二叉树. 右子树是通过数组中最 ...

  2. Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree)

    Leetcode之分治法专题-654. 最大二叉树(Maximum Binary Tree) 给定一个不含重复元素的整数数组.一个以此数组构建的最大二叉树定义如下: 二叉树的根是数组中的最大元素. 左 ...

  3. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

  4. 【Leetcode】104. 二叉树的最大深度

    题目 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,null,15,7 ...

  5. [LeetCode] 654. Maximum Binary Tree 最大二叉树

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

  6. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

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

  7. [LeetCode]654. Maximum Binary Tree最大堆二叉树

    每次找到数组中的最大值,然后递归的构建左右树 public TreeNode constructMaximumBinaryTree(int[] nums) { if (nums.length==0) ...

  8. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

随机推荐

  1. 弹出页面第一次加载可以生成table和方法的绑定,第二次点击进来不能生成table和方法的帮定

    问题原因: 弹出页面的写法是每次点击都会在原有页面基础之上新添加一个将其覆盖,原有页面不关闭.我用的生成table和点击事件的绑定是id选择器.页面中只绑定第一次的页面,第二次的页面作用不上. 解决: ...

  2. Java生成不重复的随机数

    public class test { public static int[] Randoms(int number) { Random rand = new Random(); //创建一个新随机数 ...

  3. 【BZOJ2730】[HNOI2012] 矿场搭建(找割点)

    点此看题面 大致题意: 一张无向图,要求你在去掉任意一个节点之后,剩余的每个节点都能到达一个救援出口,问至少需要几个救援出口. 第一步:\(Tarjan\)求割点 首先,我们要跑一遍\(Tarjan\ ...

  4. linux 查看帐号创建时间

    查看用户的home目录的创建时间 查看日志 用stat 命令,可以看到目录的三个时间.不过这个时间只是用来参考的,确定一个范围. 查看日志是最准确的方法 /var/log/auth.log ,前提是你 ...

  5. python剑指offer 链表中环的入口节点

    题目: 一个链表中包含环,请找出该链表的环的入口结点. 思路: 先说个定理:两个指针一个fast.一个slow同时从一个链表的头部出发, fast一次走2步,slow一次走一步,如果该链表有环,两个指 ...

  6. python_40_通过脚本转换参数实现替换

    import sys f=open('yesterday','r',encoding='utf-8') f_new=open('yesterday_update','w',encoding='utf- ...

  7. C# 创建子目录

    运用DirectoryInfo类创建子目录是非常容易的,你只要调用其中CreateSubdirectory()方法即可,演示代码如下. DirectoryInfo dir = new Director ...

  8. 国产中标麒麟Linux部署dotnet core 环境并运行项目 (三) 部署运行WEB API项目

    部署dotnet Core Web API 上一步的文章,是我们公司最核心的一个ORM组件,在中标麒麟系统完成了一个插入数据的任务,这一步是将正式的从dot net framework 迁移到 dot ...

  9. jquery简易的三级导航

    <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">   ...

  10. JavaScript两种创建标签的的方法,实现点击按钮让text自增

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...