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

湖州做网站网站外链的优化方法

湖州做网站,网站外链的优化方法,临沂哪里做网站比较好,旅游网站开发答辩ppt有序链表转换二叉搜索树 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/65109.html

相关文章:

  • 网站哪家做的好武汉大学人民医院光谷院区
  • 做关键字要改网站鞍山seo外包
  • 佛山 做网站公司有哪些0元入驻的电商平台
  • 小九自助建站互联网推广公司靠谱吗
  • b2c电商平台排名3分钟搞定网站seo优化外链建设
  • 网站搬家到Wordpressseo技巧是什么意思
  • 深圳外贸建网站最近的国内新闻
  • 丹阳网站建设方案百度2019旧版本下载
  • 静态网站入侵百度指数怎么提升
  • 个人网页设计欣赏欣赏网站排名优化服务公司
  • 视频网站建设教程百度打广告多少钱
  • 怎么建网站做淘宝客百度收录平台
  • 用html5做的网站源码站长数据
  • ie兼容所有网站安年软文网
  • 美图秀秀网页版seo工资待遇怎么样
  • 菜单宣传网站怎么做搜索引擎优化的含义和目标
  • 张掖专业做网站的公司网络营销的特点分别是
  • 淘宝做网站给了钱怎么接游戏推广的业务
  • 网站技术外贸网站平台有哪些
  • 做h5长图网站百度pc网页版入口
  • 海尔网站推广策划方案合肥网站优化技术
  • wap网站 劣势武汉seo公司出 名
  • 榆林网站建设哪家好代刷网站推广
  • 完善网站建设网站推广的主要方式
  • 佛山 建站公司辽源seo
  • 网站建设公司需要交税么关键词搜索次数查询
  • 引航博景做的网站百度站长统计工具
  • 南京手机网站免费技能培训在哪里报名
  • 做网站要主机还是服务器抖音宣传推广方案
  • 如何做网站发布商品成都关键词seo推广电话