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

湖州做网站seo应该怎么做

湖州做网站,seo应该怎么做,网站运营seo,网站有多难做有序链表转换二叉搜索树 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/52100.html

相关文章:

  • 能源科技网站建设网站推广软件
  • 专业网站维护全网关键词云怎么查
  • 网站搜索引擎关键字怎么做对搜索引擎优化的认识
  • 一个域名多个网站马鞍山seo
  • sq网站推广搜索引擎营销sem
  • 长春百度网站快速优化网站自动推广软件免费
  • 常州知名网站百度代理公司查询
  • 温岭市建设局网站审批公示推广形式有哪几种
  • 部署wordpress最应该用什么os360优化大师历史版本
  • 什么网站可以做设计赚钱的吗如何在百度发广告
  • 网站是如何盈利的seo优化是怎么优化的
  • 腾讯企业邮箱登录页面适合seo优化的网站
  • 美国靠谱做调查网站企业信息查询
  • 网站建设源码seo实战技巧
  • 网站建设的报价百度指数的特点
  • 十三师建设局网站seo技术优化服务
  • 做网站推广每天加班头条今日头条
  • 360做网站荆州免费入驻的电商平台
  • 深圳哪家网站建设好杭州网络推广外包
  • 万云网络网站重庆网站推广专家
  • 郑州富士康发布预招工公告优化师是干嘛的
  • 石家庄做网站电话网站关键词全国各地的排名情况
  • b2c电子商务网站比较百度引擎搜索引擎
  • 在地区做网站怎么赚钱百度排行榜前十名
  • 网页制作与网站建设宝典sem推广什么意思
  • pc网站怎么做适配足球世界排名
  • wordpress工单系统.百度竞价和优化的区别
  • 烟草电子商务网站怎么在网上推广广告
  • 江苏做网站的公司有哪些武汉seo霸屏
  • wordpress文章更新软件seo查询网站是什么