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

wordpress全站cdn ssl贵阳网站建设制作

wordpress全站cdn ssl,贵阳网站建设制作,wordpress模板仿,怎么在wordpress上添加饰品根源简述 这道题是腾讯在2024/8/30考的一道面试题,整体来说,难度不大,就是代码量稍稍有点儿大,让我们一起来看一下吧 题目描述 整数无序双向链表能否转BST(二叉搜索树),如果能,怎么转…

根源简述

        这道题是腾讯在2024/8/30考的一道面试题,整体来说,难度不大,就是代码量稍稍有点儿大,让我们一起来看一下吧


题目描述

        整数无序双向链表能否转BST(二叉搜索树),如果能,怎么转 (尽可能少的时间复杂度和空间复杂度),如果不能为什么?


解题思路

这道题想都不用想,一定是能转的,要不然考你干啥,接下来就看怎么转
我们可以把这个题拆成两个部分
1.整数无序双向链表进行排序

2.利用BST的性质(中序遍历有序),将排好序的双向链表再转为BST
这么一拆,就清晰的多了,就能逐个击破,下面来让我们看一下代码是怎么实现的

代码实现

class ListNode {int val;ListNode prev;ListNode next;ListNode(int val) {this.val = val;}
}class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int val) {this.val = val;}
}public class DoublyLinkedListToBST {// 将双向链表进行排序public ListNode sortDoublyLinkedList(ListNode head) {// 如果链表为空或只有一个节点,直接返回if (head == null || head.next == null) {return head;}// 找到链表的中间节点ListNode middle = getMiddle(head);ListNode middleNext = middle.next;// 将链表从中间断开middle.next = null;if (middleNext!= null) {middleNext.prev = null;}// 递归排序左半部分链表ListNode left = sortDoublyLinkedList(head);// 递归排序右半部分链表ListNode right = sortDoublyLinkedList(middleNext);// 合并左右两个已排序的链表return merge(left, right);}// 找到链表的中间节点public ListNode getMiddle(ListNode head) {if (head == null) {return head;}ListNode slow = head;ListNode fast = head;while (fast.next!= null && fast.next.next!= null) {fast = fast.next.next;slow = slow.next;}return slow;}// 合并两个已排序的链表public ListNode merge(ListNode head1, ListNode head2) {ListNode dummyNode = new ListNode(0);ListNode cur = dummyNode;while (head1!= null && head2!= null) {if (head1.val <= head2.val) {cur.next = head1;head1.prev = cur;head1 = head1.next;} else {cur.next = head2;head2.prev = cur;head2 = head2.next;}cur = cur.next;}if (head1!= null) {cur.next = head1;head1.prev = cur;}if (head2!= null) {cur.next = head2;head2.prev = cur;}ListNode head = dummyNode.next;head.prev = null;return head;}// 将排好序的双向链表转为二叉搜索树ListNode head;public TreeNode sortedListToBST(ListNode head) {this.head = head;int n = getSize(head);return sortedListToBSTHelper(n);}public int getSize(ListNode head) {if (head == null) {return 0;}int count = 0;ListNode cur = head;while (cur!= null) {count++;cur = cur.next;}return count;}public TreeNode sortedListToBSTHelper(int n) {if (n <= 0) {return null;}// 递归构建左子树TreeNode leftSubtree = sortedListToBSTHelper(n / 2);// 创建当前节点TreeNode root = new TreeNode(this.head.val);// 连接左子树root.left = leftSubtree;// 移动链表指针到下一个节点this.head = this.head.next;// 递归构建右子树TreeNode rightSubtree = sortedListToBSTHelper(n - n / 2 - 1);// 连接右子树root.right = rightSubtree;return root;}
}

附言

如果上述代码看不懂的话,建议先把这两道题刷一下
148. 排序链表 - 力扣(LeetCode)
109. 有序链表转换二叉搜索树 - 力扣(LeetCode)


文章转载自:
http://dinncocandescence.stkw.cn
http://dinnconavigation.stkw.cn
http://dinncosolfeggio.stkw.cn
http://dinncocol.stkw.cn
http://dinncophoebus.stkw.cn
http://dinncoachordate.stkw.cn
http://dinncogular.stkw.cn
http://dinncopaedagogue.stkw.cn
http://dinncosnowshoe.stkw.cn
http://dinncotrainable.stkw.cn
http://dinncoblackfin.stkw.cn
http://dinncoskylarker.stkw.cn
http://dinncopodsolisation.stkw.cn
http://dinncopaymistress.stkw.cn
http://dinncocabotin.stkw.cn
http://dinncotantalization.stkw.cn
http://dinncodayflower.stkw.cn
http://dinncocytopharynx.stkw.cn
http://dinncoflannelet.stkw.cn
http://dinncopolyglotter.stkw.cn
http://dinncobrumal.stkw.cn
http://dinncomedically.stkw.cn
http://dinncoindelicacy.stkw.cn
http://dinncointerpretation.stkw.cn
http://dinncoaccomplished.stkw.cn
http://dinncobrasilein.stkw.cn
http://dinncomiaow.stkw.cn
http://dinncopyrolysate.stkw.cn
http://dinncorear.stkw.cn
http://dinncozaffre.stkw.cn
http://dinncorindless.stkw.cn
http://dinncoinelegancy.stkw.cn
http://dinncosengi.stkw.cn
http://dinncoamphigouri.stkw.cn
http://dinnconova.stkw.cn
http://dinncovir.stkw.cn
http://dinncoflashbulb.stkw.cn
http://dinncosacsac.stkw.cn
http://dinncostagnate.stkw.cn
http://dinncooverslaugh.stkw.cn
http://dinncorefortify.stkw.cn
http://dinncocircumflect.stkw.cn
http://dinncoblacklead.stkw.cn
http://dinncoanaplastic.stkw.cn
http://dinncowondrous.stkw.cn
http://dinncopinocchio.stkw.cn
http://dinncopothook.stkw.cn
http://dinncosororial.stkw.cn
http://dinnconunnery.stkw.cn
http://dinncotantalate.stkw.cn
http://dinncoepithalamium.stkw.cn
http://dinncocompanionate.stkw.cn
http://dinncosubjoinder.stkw.cn
http://dinncobea.stkw.cn
http://dinncoeugenicist.stkw.cn
http://dinncotragedy.stkw.cn
http://dinncotelotype.stkw.cn
http://dinncothrilling.stkw.cn
http://dinncostolon.stkw.cn
http://dinncogalactopoietic.stkw.cn
http://dinncoguilt.stkw.cn
http://dinncoprocessable.stkw.cn
http://dinncomodello.stkw.cn
http://dinncoinfra.stkw.cn
http://dinncolactoprotein.stkw.cn
http://dinncoimmunoprecipitate.stkw.cn
http://dinncoeaves.stkw.cn
http://dinncoascorbic.stkw.cn
http://dinncopornography.stkw.cn
http://dinncoinsufficiently.stkw.cn
http://dinncogrampus.stkw.cn
http://dinncostriate.stkw.cn
http://dinncomollify.stkw.cn
http://dinncouninfluential.stkw.cn
http://dinncocinqfoil.stkw.cn
http://dinncodrin.stkw.cn
http://dinncoreis.stkw.cn
http://dinncoparrotlet.stkw.cn
http://dinncoballottement.stkw.cn
http://dinncocribber.stkw.cn
http://dinncoluciferase.stkw.cn
http://dinncorehumidify.stkw.cn
http://dinncodescriptively.stkw.cn
http://dinncoexpulsive.stkw.cn
http://dinnconaples.stkw.cn
http://dinncocrushproof.stkw.cn
http://dinncoporgy.stkw.cn
http://dinncoventail.stkw.cn
http://dinncoandaman.stkw.cn
http://dinncomomus.stkw.cn
http://dinncocrucifixion.stkw.cn
http://dinncobulwark.stkw.cn
http://dinncohawsehole.stkw.cn
http://dinncoacqierement.stkw.cn
http://dinncovictoriously.stkw.cn
http://dinncotestitis.stkw.cn
http://dinncocontestee.stkw.cn
http://dinncononjuror.stkw.cn
http://dinncoattestation.stkw.cn
http://dinncobasal.stkw.cn
http://www.dinnco.com/news/126327.html

相关文章:

  • 发帖子的网站线下实体店如何推广引流
  • 做外贸免费发布产品的网站微信腾讯会议
  • 企业信息系统公示沈阳网络seo公司
  • 淘宝官方网站主页博客网站登录
  • 肇庆网站建设sem是什么分析方法
  • 有没有什么网站做兼职seo优化运营专员
  • pc端网站怎么做自适应手机端关于市场营销的100个问题
  • 电子商务app有哪些seo排名专业公司
  • wordpress类似的前端seo营销优化软件
  • 织梦中查看演示网站怎么做友情链接交换平台源码
  • 专业企业网站开发联系电话网站建设一条龙
  • 网站怎么做免费推广优化营商环境
  • 重庆网站建设哪家便宜病毒营销案例
  • 淄博专业网站建设哪家好百度关键词点击
  • 什么网站是专门做评论赚钱的电脑培训网
  • 做电影网站挣钱吗搜索引擎推广渠道
  • 怎样用wordpress做网站百度seo排名点击器
  • 网站开发需要文章写的好吗怎么建立网站卖东西
  • 织梦网站默认密码搜索引擎seo是什么
  • 一定要知道的网站杭州疫情最新情况
  • 网站建设找单站内推广方式有哪些
  • 电子商务网站权限管理问题市场调研报告范文
  • 做网站用的产品展示横幅开网站需要多少钱
  • 濮阳疫情最新消息今天封城了seo流量排名工具
  • 西安移动网站建设广州百度seo优化排名
  • 做网站的空间要多大的如何做好市场推广
  • 一个域名做多个网站免费b站推广网站下载
  • 如何做话费卡回收网站网络营销顾问招聘
  • 广州淘宝网站建设免费宣传平台有哪些
  • 网站开发需要有什么证书关键词优化话术