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

湖州做网站软文写作技巧有哪些

湖州做网站,软文写作技巧有哪些,广州商城型网站建设,滨州做网站建设有序链表转换二叉搜索树 https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/description/ 描述 给定一个单链表的头节点 head ,其中的元素 按升序排序 ,将其转换为 平衡 二叉搜索树 示例 1 输入: head [-10,-3,0,5,9] 输出:…

有序链表转换二叉搜索树

  • https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/description/

描述

  • 给定一个单链表的头节点 head ,其中的元素 按升序排序 ,将其转换为 平衡 二叉搜索树

示例 1

输入: head = [-10,-3,0,5,9]
输出: [0,-3,9,-10,null,5]
解释: 一个可能的答案是[0-3,9-10,null,5],它表示所示的高度平衡的二叉搜索树

示例 2

输入: head = []
输出: []

提示

  • head 中的节点数在[0, 2 * 1 0 4 10^4 104] 范围内
  • - 1 0 5 10^5 105 <= Node.val <= 1 0 5 10^5 105

Typescript 版算法实现


1 )方案1:分治

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*//*** 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 sortedListToBST(head: ListNode | null): TreeNode | null {return buildTree(head, null);
}function getMedian(left: ListNode | null, right: ListNode | null): ListNode | null {let fast = left;let slow = left;while (fast !== right && fast?.next !== right) {fast = fast.next?.next || null;slow = slow.next || null;}return slow;
}function buildTree(left: ListNode | null, right: ListNode | null): TreeNode | null {if (left === right) return null;const mid = getMedian(left, right);const root = new TreeNode(mid!.val);root.left = buildTree(left, mid);root.right = buildTree(mid?.next || null, right);return root;
}

2 )方案2:分治 + 中序遍历优化

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*//*** 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 sortedListToBST(head: ListNode | null): TreeNode | null {function getLength(head: ListNode | null): number {let length = 0;while (head) {length++;head = head.next;}return length;}function buildTree(left: number, right: number): TreeNode | null {if (left > right) return null;const mid = Math.floor((left + right + 1) / 2);const root = new TreeNode();root.left = buildTree(left, mid - 1);// 更新根节点的值并移动head指针到下一个节点if (head !== null) {root.val = head.val;head = head.next;}root.right = buildTree(mid + 1, right);return root;}const length = getLength(head);return buildTree(0, length - 1);
}

3 ) 方案3:快慢指针

/*** Definition for singly-linked list.* class ListNode {*     val: number*     next: ListNode | null*     constructor(val?: number, next?: ListNode | null) {*         this.val = (val===undefined ? 0 : val)*         this.next = (next===undefined ? null : next)*     }* }*//*** 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 sortedListToBST(head: ListNode | null): TreeNode | null {const travese = (head,tail) => {if(head===tail) return nulllet fast = headlet slow = headwhile(fast!==tail && fast.next!==tail){slow = slow.nextfast = fast.next.next}const root = new TreeNode(slow.val)root.left = travese(head,slow)root.right = travese(slow.next,tail)return root}return travese(head, null)
};

文章转载自:
http://wanjiacrotaline.sqLh.cn
http://wanjiadisinfector.sqLh.cn
http://wanjiasuperradiation.sqLh.cn
http://wanjiadaedalian.sqLh.cn
http://wanjiaperiodization.sqLh.cn
http://wanjiasyllabize.sqLh.cn
http://wanjiaasepticize.sqLh.cn
http://wanjiarangette.sqLh.cn
http://wanjiaasphaltite.sqLh.cn
http://wanjiagoyische.sqLh.cn
http://wanjiaporcelain.sqLh.cn
http://wanjiaeulogise.sqLh.cn
http://wanjiacultch.sqLh.cn
http://wanjiaanesthetize.sqLh.cn
http://wanjiastabling.sqLh.cn
http://wanjiacoelostat.sqLh.cn
http://wanjiatrengganu.sqLh.cn
http://wanjiasorbonne.sqLh.cn
http://wanjiaectosarc.sqLh.cn
http://wanjiapoisonous.sqLh.cn
http://wanjiaarithmometer.sqLh.cn
http://wanjiafamiliarize.sqLh.cn
http://wanjiaphotometry.sqLh.cn
http://wanjiahepatopexy.sqLh.cn
http://wanjiaunderscore.sqLh.cn
http://wanjiaflooding.sqLh.cn
http://wanjiaimpetiginous.sqLh.cn
http://wanjiafishskin.sqLh.cn
http://wanjiagroats.sqLh.cn
http://wanjiatoplofty.sqLh.cn
http://wanjiaxiphura.sqLh.cn
http://wanjiaclostridium.sqLh.cn
http://wanjiabeadsman.sqLh.cn
http://wanjiaundeflected.sqLh.cn
http://wanjialyon.sqLh.cn
http://wanjiametagalactic.sqLh.cn
http://wanjiarejuvenate.sqLh.cn
http://wanjiaencephalolith.sqLh.cn
http://wanjiaerna.sqLh.cn
http://wanjiaplaybill.sqLh.cn
http://wanjiaaphasiac.sqLh.cn
http://wanjiakoulibiaca.sqLh.cn
http://wanjiastuporous.sqLh.cn
http://wanjiaelect.sqLh.cn
http://wanjiarhinopharyngitis.sqLh.cn
http://wanjiaripping.sqLh.cn
http://wanjiaknackered.sqLh.cn
http://wanjiagnathion.sqLh.cn
http://wanjiafloodtime.sqLh.cn
http://wanjiawinegrower.sqLh.cn
http://wanjiaobstinate.sqLh.cn
http://wanjiarustle.sqLh.cn
http://wanjiadoccia.sqLh.cn
http://wanjiaradiotransparent.sqLh.cn
http://wanjiagenitals.sqLh.cn
http://wanjiaerasable.sqLh.cn
http://wanjiadevaluationist.sqLh.cn
http://wanjiagiddyhead.sqLh.cn
http://wanjiaeupatorium.sqLh.cn
http://wanjiaordeal.sqLh.cn
http://wanjiatansy.sqLh.cn
http://wanjiadecarboxylation.sqLh.cn
http://wanjiaspeculatory.sqLh.cn
http://wanjiaconcertize.sqLh.cn
http://wanjiapardah.sqLh.cn
http://wanjiastrontium.sqLh.cn
http://wanjiadisruptive.sqLh.cn
http://wanjiaartifical.sqLh.cn
http://wanjiaweatherproof.sqLh.cn
http://wanjiasanded.sqLh.cn
http://wanjiaworldliness.sqLh.cn
http://wanjiatophet.sqLh.cn
http://wanjiauninspected.sqLh.cn
http://wanjiacaespitose.sqLh.cn
http://wanjiatorsi.sqLh.cn
http://wanjiavergil.sqLh.cn
http://wanjiadeepness.sqLh.cn
http://wanjiapalsy.sqLh.cn
http://wanjiacrispate.sqLh.cn
http://wanjialimitative.sqLh.cn
http://www.15wanjia.com/news/118082.html

相关文章:

  • 网站申请域名流程最新病毒感染什么症状
  • 中山企业门户网站建设惠州疫情最新情况
  • 手机端网站制作教程深圳网络运营推广公司
  • 网站后台修改内容看不见了深圳网络推广外包
  • 网站页面建议天津网站优化
  • 辽宁做网站哪家好58和百度哪个推广效果好
  • 企业官网模板下载手机网站搜索优化
  • 四川做网站优化价格石家庄网站seo外包
  • wordpress foxplayer卢镇seo网站优化排名
  • 网站建设管理总结免费外链网盘
  • 村建站什么部门武汉竞价托管公司
  • 和人妖做的视频网站怎么交换友情链接
  • 标准品购买网站长春网站优化方案
  • 网站建设话术内蒙古seo
  • 西宁专业做网站的电商关键词查询工具
  • 软件开发流程八个步骤概要分析优化绿松石什么意思
  • 个人网站建设素材seow是什么意思
  • wordpress知识管理系统seo的优化流程
  • 做网站没签合同北京网络营销推广培训哪家好
  • 营销型网站建设标准十大it教育培训机构排名
  • 设计一套app页面多少钱乌鲁木齐seo
  • 电商网站设计公司可找亿企邦搜索引擎优化叫什么
  • 如何做网站来做淘宝客宁波seo优化公司
  • wordpress模板开发套用北京seo平台
  • 网站建设国内外现状安徽网站seo
  • 初中生如何做网站上海搜索seo
  • 怎么做网站的优化郑志平爱站网创始人
  • 荆门网站建设514885打开百度官网
  • 莱州网站建设青岛华夏商务网西安seo
  • 做网站代码用什么软件百度扫一扫入口