构建一颗「最大树」。 注意consruct的时候最后的return root; 我参考了serialize and deserialize binary tree的build tree 的过程。

这周contest的第二题,一次AC了还挺开心的,而且用的时间不长。我发现当你没思路的时候或是思维陷入死循环jiang化的时候时间过得特别快,有思路就不一样。

   public TreeNode constructMaximumBinaryTree(int[] nums) {return construct(nums, 0, nums.length - 1);}private TreeNode construct(int[] nums, int i, int j) {if (i > j) return null;int[] arr = findMax(nums, i, j);TreeNode root = new TreeNode(arr[0]);root.left = construct(nums, i, arr[1] - 1);root.right = construct(nums, arr[1] + 1, j);return root;}private int[] findMax(int[] nums, int i, int j) {int[] res = new int[2];res[0] = Integer.MIN_VALUE;for (int k = i; k <= j; k++) {if (nums[k] > res[0]) {res[0] = nums[k];res[1] = k;}}return res;}
复制代码

转载于:https://juejin.im/post/5a31340a6fb9a045023ba3b7

Construct Maximum Binary Tree相关推荐

  1. Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造

    传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...

  2. 【leetcode】654. Maximum Binary Tree

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

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

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

  4. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  5. 654. Maximum Binary Tree最大二叉树

    网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...

  6. Leetcode-998 Maximum Binary Tree II(最大二叉树 II)

    作者水平有限,所发仅为个人愚见,如有明显谬误,望斧正 此题给出了一棵二叉树,并给出了此二叉树的构建方法,要求coder将给出的二叉树还原成一维vector后,加上一个新的val,并再按照题目所给构建方 ...

  7. 【CF1311E】Construct the Binary Tree【增量构造】【复杂度证明】

    题意:给定nnn和ddd,构造或判断无法构造一棵二叉树使得所有点的深度(定义为到根距离)之和为ddd. n,d≤5000n,d\leq 5000n,d≤5000 显然可以算出有解的ddd的下界和上界, ...

  8. Given inorder and postorder traversal of a tree, construct the binary tree

    输入某二叉树的中序遍历和后序遍历的结果,请重建出该二叉树. 例如如下二叉树 中序:8 4 2 5 1 6 9 3 7 后序:8 4 5 2 9 6 7 3 1 后序的最后一个节点为根节点,在中序遍历中 ...

  9. leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)...

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

最新文章

  1. Qt分析:Qt中的两种定时器
  2. HTTP中response响应数据获取
  3. 允许活动内容如脚本和activex控件
  4. 洛谷P3066 [USACO12DEC]逃跑的BarnRunning Away From…
  5. Spring Aop总结
  6. stm32f407能跑linux吗_跑步能跑进医院?那我该做跑步运动吗?想健康一点太难了...
  7. openwrt 19 overlay 空间不足_【邯郸】2020年度市级科技企业孵化器和众创空间拟认定名单公示...
  8. linux实现内存共享,Linux共享内存实现
  9. nginx 实现负载均衡
  10. php怎么代表不同行,php – 单击按钮时显示数据库的不同行
  11. 谈谈JS中的函数节流
  12. Linux下MySQL的基础(一)
  13. c语言课程设计 工资管理系统
  14. SpringBoot 动态设置响应头的content-type
  15. 垂直广告是什么意思_广告新趋势——内容为王 垂直细分
  16. 关于计算机系统的夺命12问,你能坚持到第几问?
  17. 解决:RN和H5之间通信
  18. 服务器时间修改连接超时时间设置,云服务器连接超时时间设置
  19. Ubuntu 16.04 鼠标可以移动不能点击问题
  20. 回环口--Loopback接口

热门文章

  1. 曼大和华威计算机科学预科哪个好,华威预科申请曼彻斯特本科
  2. java 创建restful_使用Java创建RESTful Web Service
  3. android自定义view实现原理,android随笔之自定义View基本原理
  4. leetcode刷题:求旋转有序数组的最小值
  5. arduino点阵声音频谱_【紫歌のarduino笔记】基于FFT的音乐频谱显示器
  6. 存定期还能加钱进去吗_购买支付宝的定期理财产品,会亏钱吗?
  7. java 泛型 泛型接口(Generic Interfaces)类型形参(Type Parameters)
  8. Elasticsearch SQL插件
  9. Pandas to_timedelta
  10. flask-前端-requests之response对应关系 img