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

数据结构与算法之二叉树: LeetCode 109. 有序链表转换二叉搜索树 (Ts版)

有序链表转换二叉搜索树

  • 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.lryc.cn/news/517475.html

相关文章:

  • Android NDK开发入门2之适应idm环境
  • 如何隐藏 Nginx 版本号 并自定义服务器信息,提升安全性
  • 鸿蒙的APP真机调试以及发布
  • 图像处理|膨胀操作
  • 攻防世界 ics-07
  • C# 之某度协议登录,JS逆向,手机号绑定,获取CK
  • js适配器模式
  • 小徐影城管理系统(源码+数据库+文档)
  • Linux第101步_了解LCD屏驱动“panel-simple.c”
  • 【实用技能】如何使用 .NET C# 中的 Azure Key Vault 中的 PFX 证书对 PDF 文档进行签名
  • 前端基础函数算法整理应用(sort+reduce+date+双重for循环)
  • 鸿蒙MPChart图表自定义(六)在图表中绘制游标
  • poi-tl+kkviewfile实现生成pdf业务报告
  • 【Uniapp-Vue3】scroll-view可滚动视图区域组件
  • asp.net core webapi中的数据注解与数据验证
  • PixPin—— 高效截图工具的下载与使用攻略
  • Go语言的 的多态性(Polymorphism)基础知识
  • Vue框架主要用来做什么?Vue框架的好处和特性.
  • 科普CMOS传感器的工作原理及特点
  • tensorflow 内存错误
  • spring boot解决swagger中的v2/api-docs泄露漏洞
  • 计算机网络 (25)IPV6
  • 小程序组件 —— 30 组件 - 背景图片的使用
  • 《Opencv》信用卡信息识别项目
  • Matlab贝叶斯估计MCMC分析药物对不同种群生物生理指标数据评估可视化
  • java 转义 反斜杠 Unexpected internal error near index 1
  • 网络安全常见的问题
  • 在ubuntu22.04中使用bear命令追踪内核编译报错的原因分析和解决方案
  • 【软考网工笔记】操作系统管理与配置——Windows
  • vue3 css实现文字输出带光标显示,文字输出完毕,光标消失的效果