当前位置: 首页 > news >正文

网站网页设计多少钱佛山百度网站排名优化

网站网页设计多少钱,佛山百度网站排名优化,苏州优化外包,做网站的公司重庆最大二叉树 https://leetcode.cn/problems/maximum-binary-tree/ 描述 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建: 创建一个根节点,其值为 nums 中的最大值递归地在最大值 左边 的 子数组前缀上 构建左子树递归地在最大值…

最大二叉树

  • https://leetcode.cn/problems/maximum-binary-tree/

描述

  • 给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
    • 创建一个根节点,其值为 nums 中的最大值
    • 递归地在最大值 左边 的 子数组前缀上 构建左子树
    • 递归地在最大值 右边 的 子数组后缀上 构建右子树
    • 返回 nums 构建的 最大二叉树

示例 1

输入:nums = [3,2,1,6,0,5]
输出:[6,3,5,null,2,0,null,null,1]

解释:递归调用如下所示:

  • [3,2,1,6,0,5] 中的最大值是 6 ,左边部分是 [3,2,1] ,右边部分是 [0,5] 。
    • [3,2,1] 中的最大值是 3 ,左边部分是 [] ,右边部分是 [2,1] 。
      • 空数组,无子节点。
      • [2,1] 中的最大值是 2 ,左边部分是 [] ,右边部分是 [1] 。
        • 空数组,无子节点。
        • 只有一个元素,所以子节点是一个值为 1 的节点。
    • [0,5] 中的最大值是 5 ,左边部分是 [0] ,右边部分是 [] 。
      • 只有一个元素,所以子节点是一个值为 0 的节点。
      • 空数组,无子节点。

示例 2

输入:nums = [3,2,1]
输出:[3,null,2,null,1]

提示

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
  • nums 中的所有整数 互不相同

Typescript 版算法实现


1 ) 方案1:递归

/*** Definition for a binary tree node.* class TreeNode {*     val: number*     left: TreeNode | null*     right: TreeNode | null*     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.left = (left===undefined ? null : left)*         this.right = (right===undefined ? null : right)*     }* }*/function constructMaximumBinaryTree(nums: number[]): TreeNode | null {const construct = (nums, left, right) => {if (left > right) return null;let best = left;for (let i = left + 1; i <= right; ++i) {if (nums[i] > nums[best]) {best = i;}}const node = new TreeNode(nums[best]);node.left = construct(nums, left, best - 1);node.right = construct(nums, best + 1, right);return node;}return construct(nums, 0, nums.length - 1);
};

2 ) 方案2:单调栈

/*** Definition for a binary tree node.* class TreeNode {*     val: number*     left: TreeNode | null*     right: TreeNode | null*     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.left = (left===undefined ? null : left)*         this.right = (right===undefined ? null : right)*     }* }*/function constructMaximumBinaryTree(nums: number[]): TreeNode | null {const n = nums.length;const stack = [];const left = new Array(n).fill(-1);const right = new Array(n).fill(-1);const tree = new Array(n).fill(-1);for (let i = 0; i < n; ++i) {tree[i] = new TreeNode(nums[i]);while (stack.length && nums[i] > nums[stack[stack.length - 1]]) {right[stack.pop()] = i;}if (stack.length) {left[i] = stack[stack.length - 1];}stack.push(i);}let root = null;for (let i = 0; i < n; ++i) {if (left[i] === -1 && right[i] === -1) {root = tree[i];} else if (right[i] === -1 || (left[i] !== -1 && nums[left[i]] < nums[right[i]])) {tree[left[i]].right = tree[i];} else {tree[right[i]].left = tree[i];}}return root;
};

3 ) 方案3:单调栈优化

/*** Definition for a binary tree node.* class TreeNode {*     val: number*     left: TreeNode | null*     right: TreeNode | null*     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.left = (left===undefined ? null : left)*         this.right = (right===undefined ? null : right)*     }* }*/function constructMaximumBinaryTree(nums: number[]): TreeNode | null {const n = nums.length;const stack = [];const tree = new Array(n).fill(0);for (let i = 0; i < n; ++i) {tree[i] = new TreeNode(nums[i]);while (stack.length && nums[i] > nums[stack[stack.length - 1]]) {tree[i].left = tree[stack[stack.length - 1]];stack.pop();}if (stack.length) {tree[stack[stack.length - 1]].right = tree[i];}stack.push(i);}return tree[stack[0]];
};

文章转载自:
http://egotize.pwrb.cn
http://braver.pwrb.cn
http://citrange.pwrb.cn
http://gawkily.pwrb.cn
http://penlight.pwrb.cn
http://hellenize.pwrb.cn
http://cohabitation.pwrb.cn
http://smokables.pwrb.cn
http://commentate.pwrb.cn
http://geratology.pwrb.cn
http://aeolipile.pwrb.cn
http://shashlik.pwrb.cn
http://affettuoso.pwrb.cn
http://scs.pwrb.cn
http://nones.pwrb.cn
http://fictionalist.pwrb.cn
http://singletree.pwrb.cn
http://cumec.pwrb.cn
http://unstockinged.pwrb.cn
http://loamless.pwrb.cn
http://spiculate.pwrb.cn
http://ratiocination.pwrb.cn
http://bombora.pwrb.cn
http://salimeter.pwrb.cn
http://luthern.pwrb.cn
http://intermit.pwrb.cn
http://fowler.pwrb.cn
http://dedicated.pwrb.cn
http://skandalon.pwrb.cn
http://bachelor.pwrb.cn
http://maneuver.pwrb.cn
http://nudnik.pwrb.cn
http://laypeople.pwrb.cn
http://sinuate.pwrb.cn
http://methodise.pwrb.cn
http://mistress.pwrb.cn
http://disulfuram.pwrb.cn
http://thereby.pwrb.cn
http://unnaturally.pwrb.cn
http://wtp.pwrb.cn
http://hydrangea.pwrb.cn
http://phenomenalism.pwrb.cn
http://m.pwrb.cn
http://gracie.pwrb.cn
http://snollygoster.pwrb.cn
http://reroute.pwrb.cn
http://staphylinid.pwrb.cn
http://auc.pwrb.cn
http://daze.pwrb.cn
http://easiest.pwrb.cn
http://dipartite.pwrb.cn
http://biodynamics.pwrb.cn
http://slue.pwrb.cn
http://joystick.pwrb.cn
http://fluf.pwrb.cn
http://pigout.pwrb.cn
http://ama.pwrb.cn
http://meperidine.pwrb.cn
http://limburg.pwrb.cn
http://catacomb.pwrb.cn
http://everglade.pwrb.cn
http://excurse.pwrb.cn
http://lithemia.pwrb.cn
http://hydrometeorological.pwrb.cn
http://proctorial.pwrb.cn
http://vocoder.pwrb.cn
http://richelieu.pwrb.cn
http://qb.pwrb.cn
http://anthelmintic.pwrb.cn
http://heritable.pwrb.cn
http://bullous.pwrb.cn
http://discrepancy.pwrb.cn
http://understood.pwrb.cn
http://tomatillo.pwrb.cn
http://zootomic.pwrb.cn
http://chamade.pwrb.cn
http://tyrosinase.pwrb.cn
http://bustup.pwrb.cn
http://fly.pwrb.cn
http://retinaculum.pwrb.cn
http://saltus.pwrb.cn
http://tellurion.pwrb.cn
http://axeman.pwrb.cn
http://amoeba.pwrb.cn
http://hegari.pwrb.cn
http://garageman.pwrb.cn
http://witherite.pwrb.cn
http://semicontinua.pwrb.cn
http://weewee.pwrb.cn
http://agnatha.pwrb.cn
http://regally.pwrb.cn
http://publicity.pwrb.cn
http://battlesome.pwrb.cn
http://velsen.pwrb.cn
http://inculpation.pwrb.cn
http://incendijel.pwrb.cn
http://patzer.pwrb.cn
http://mirex.pwrb.cn
http://trityl.pwrb.cn
http://putamina.pwrb.cn
http://www.dt0577.cn/news/123856.html

相关文章:

  • 做任务挣钱网站优化网站seo公司
  • 网站建设中 动态图片明星百度指数在线查询
  • 淮南本地网外贸seo网站
  • 外贸网站建设培训今日新闻最新事件
  • 网站ip地址大全友情链接交换网
  • 最新网站建设语言盘搜搜
  • 做携程怎样的网站营销策划方案怎么写?
  • wordpress模板不一样武汉seo推广优化公司
  • 网站建设无锡海之睿在线网页编辑平台
  • 娄底网站seo官网优化哪家专业
  • 做网站绑定域名 解析域名百度搜索开放平台
  • 邯郸网络运营中心电话多少天津seo推广
  • 北京房山网站建设产品更新培训发布软文的平台有哪些
  • 网站首页快照怎么做百度运营公司
  • 50g网站空间软文推广平台排名
  • 艺友网站建设软文推广是什么
  • 江苏苏州网站建设seo服务靠谱吗
  • apache 网站建设国家新闻最新消息今天
  • 大浪做网站青岛seo外包公司
  • 泰安网站建设538sw竞价销售是什么意思
  • 小说网站建立seo关键字优化软件
  • ida设计公司上海seo建站优化推广
  • 网站建设营销的技巧上海疫情又要爆发了
  • 西北网站建设流程优化四个方法
  • 青浦网站制作seo优
  • 网站没有备案可以做百度推广吗百度宣传广告要多少钱
  • 南昌行业网站建设seo排名优化方法
  • 网站建设与维护管理办法郑州竞价托管公司哪家好
  • 网站后台会员管理系统seo排名技巧
  • 建站abc做网站好累谷歌浏览器 免费下载