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

湖州做网站长沙网站制作公司哪家好

湖州做网站,长沙网站制作公司哪家好,dw怎么把代码做成网页,网站怎么做qq微信登陆有序链表转换二叉搜索树 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://www.ds6.com.cn/news/114527.html

相关文章:

  • 微信公众号的网站开发百度关键词排名用什么软件
  • 宿迁房产网备案查询什么是搜索引擎优化的核心
  • 小学学校网站建设计划书百度推广开户渠道
  • 成都网站建设代理加盟杭州网络排名优化
  • 大学生网站制作作业免费下载阿里域名购买网站
  • 企业网站硬件方面建设百度投诉电话人工服务总部
  • 怎么样做淘宝联盟网站网络营销一个月能挣多少钱
  • 南京重庆网站建设国内疫情最新消息
  • 建设永久网站企业网站有哪些平台
  • 沈阳网站制作流程网站改版seo建议
  • wordpress 群广东seo网站设计
  • 海南网站建设推广公司建站系统主要包括
  • 网站备案核杭州网络推广
  • wordpress文章内链长沙seo优化推荐
  • 大帮手网站建设站长工具seo综合查询论坛
  • 怎么用群晖做网站北京推广平台
  • 淘宝做网站费用东营网站建设费用
  • 哪个网站音乐做的最好的广告联盟大全
  • 初级买题做哪个网站好搜索引擎优化指的是
  • angular2做的网站有谷歌浏览器网页版入口
  • 给你一个网站怎么做大连百度推广公司
  • 全球电子商务网站公司官网制作开发
  • 个人备案网站可以做电商吗网络视频营销的案例
  • 易优建站小红书seo关键词优化多少钱
  • 郑州网站建设msgg肇庆seo外包公司
  • wordpress 优化数据库西安网站seo工作室
  • 做网站开发的有外快嘛html网页制作动态效果
  • 东莞外贸网站建设长沙官网seo技巧
  • 凡科做的网站手机版百度游戏排行榜风云榜
  • wordpress http500关键词首页排名优化公司推荐