当前位置:网站首页>【LeetCode 剑指 Offer 36. 二叉搜索树与双向链表(中等)】
【LeetCode 剑指 Offer 36. 二叉搜索树与双向链表(中等)】
2022-04-22 23:10:00 【Minaldo7】
题目:
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。
为了让您更好地理解问题,以下面的二叉搜索树为例:

我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。
下图展示了上面的二叉搜索树转化成的链表。“head” 表示指向链表中有最小元素的节点。

特别地,我们希望可以就地完成转换操作。当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。还需要返回链表中的第一个节点的指针。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题过程:
源自LeetCode用户嗯呐整理
中序遍历,注意左右节点相连
/* // Definition for a Node. class Node { public int val; public Node left; public Node right; public Node() {} public Node(int _val) { val = _val; } public Node(int _val,Node _left,Node _right) { val = _val; left = _left; right = _right; } }; */
class Solution {
Node pre, head;
public Node treeToDoublyList(Node root) {
if(root == null) return null;
InOrder(root);
// 头尾连接
head.left = pre;
pre.right = head;
return head;
}
private void InOrder(Node root){
if(root == null)
return;
InOrder(root.left);
// 如果pre为空,就说明是第一个节点,头结点,然后用head保存头结点,用于之后的返回
if(pre==null)
head = root;
// 如果不为空,那就说明是中间的节点。并且pre保存的是上一个节点,
// 让上一个节点的右指针指向当前节点
else if(pre!=null)
pre.right = root;
// 再让当前节点的左指针指向父节点,也就连成了双向链表
root.left = pre;
// 保存当前节点,用于下层递归创建
pre = root;
InOrder(root.right);
}
}
执行结果:

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Su_Del/article/details/124354461
边栏推荐
- Read software engineering at Google (14)
- LeetCode 414. The third largest number (simple, array) day13
- Cron表达式
- The article "fluent" is used to select and upload pictures
- 2022-4-22作业-MySQL单表查询
- SystemVerilog 验证-测试平台编写指南学习笔记(3):连接设计和测试平台
- Dart:在循环中使用 Async 和 Await
- SQL Net message from client 事件产生的原因分析
- 常用搜索引擎及语法
- 伦敦金在哪里开户安全些?
猜你喜欢

WebRTC系列-WebRTC基础(七)NAT、stun和turn(2)

Redis部署

C add log4net log (console and file) to console application

企业微信扫码登录后台管理系统,如何修改二维码样式
[chestnut sugar GIS] the essence of programming - (video notes)

Yolov1 paper notes

English | day13,14 x sentence true research daily sentence (parallel and nested structure)

如何快速转载CSDN中的博客

. net 6 when exiting the constructor, the non nullable property "XXX" must contain a non null value.
![[ZJCTF 2019]NiZhuanSiWei](/img/fc/86b807e59f456e3eeec263e1d2927e.png)
[ZJCTF 2019]NiZhuanSiWei
随机推荐
cache与buffer
Detailed explanation of Gentoo system installation steps
Go语言-使用协程高效计算0-2000内每个数的累加
public speaking
减治思想——二分查找详细总结
【毕业设计】答 辩 技 巧 一(以一个过来人的身份,祝各位答辩 过 过 过)
Sequel: a few simple but useful words
SystemVerilog 验证-测试平台编写指南学习笔记(2):过程语句和子程序
[BJDCTF2020]Easy MD5
How to modify the QR code style when scanning the code and logging in the background management system of enterprise wechat
在线YAML转XML工具
Clustering data generation function -- make_ blobs()
Online yaml to XML tool
[HCTF 2018]admin之Flask之session伪造
bullwhip effect
Future source code | Professor Wu Enda's lecture: Tips for using a data centric AI approach
Golang中json.Marshal避坑
存储器简介
旅行箱指纹锁方案pcb电路板方案
yolov1论文笔记摘抄