網(wǎng)站首頁 編程語言 正文
二叉搜索樹轉(zhuǎn)換成雙向循環(huán)鏈表
本文解法基于性質(zhì):二叉搜索樹的中序遍歷為 遞增序列 。
將二叉搜索樹 轉(zhuǎn)換成一個 “排序的循環(huán)雙向鏈表”,其中包含三個要素:
1.排序鏈表:節(jié)點(diǎn)應(yīng)從小到大排序,因此應(yīng)使用 中序遍歷
2.“從小到大”訪問樹的節(jié)點(diǎn)。雙向鏈表:在構(gòu)建相鄰節(jié)點(diǎn)的引用關(guān)系時,設(shè)前驅(qū)節(jié)點(diǎn) pre 和當(dāng)前節(jié)點(diǎn) cur ,
不僅應(yīng)構(gòu)建 pre.right= cur,也應(yīng)構(gòu)建 cur.left = pre 。
3.循環(huán)鏈表:設(shè)鏈表頭節(jié)點(diǎn) head 和尾節(jié)點(diǎn) tail,則應(yīng)構(gòu)建 head.left = tail 和 tail.right = head 。
雙指針:
class Solution {
private:
Node* head, * pre = NULL;
public:
Node* treeToDoublyList(Node* root) {//雙指針做法
if (!root) return NULL;
inorder(root);
head->left = pre;
pre->right = head;
return head;
}
void inorder(Node* root)
{
if (root == NULL)return;
inorder(root->left);
root->left = pre;
if (pre)
pre->right = root;
else head = root;
pre = root;
inorder(root->right);
}
};
數(shù)組方法:很簡單就不做介紹了,就是先把節(jié)點(diǎn)都放進(jìn)數(shù)組然后在建立聯(lián)系。
Node* treeToDoublyList(Node* root) {
if (!root) return NULL;
vector<Node*> nodelist;
inorder(nodelist, root);
int l = nodelist.size();
if (l == 1)
{
nodelist[0]->right = nodelist[0];
nodelist[0]->left = nodelist[0];
return nodelist[0];
}
for (int i = 1; i < l - 1; i++) {
nodelist[i]->left = nodelist[i - 1];
nodelist[i]->right = nodelist[i + 1];
}
nodelist[0]->left = nodelist[l - 1];
nodelist[0]->right = nodelist[1];
nodelist[l - 1]->right = nodelist[0];
nodelist[l - 1]->left = nodelist[l - 2];
return nodelist[0];
}
void inorder(vector<Node*>& nodelist, Node* root)
{
if (root == NULL)return;
inorder(nodelist, root->left);
nodelist.push_back(root);
inorder(nodelist, root->right);
}
二叉搜索樹與雙向鏈表(C++中等區(qū))
題目:輸入一棵二叉搜索樹,將該二叉搜索樹轉(zhuǎn)換成一個排序的循環(huán)雙向鏈表。要求不能創(chuàng)建任何新的節(jié)點(diǎn),只能調(diào)整樹中節(jié)點(diǎn)指針的指向。
為了讓您更好地理解問題,以下面的二叉搜索樹為例:
我們希望將這個二叉搜索樹轉(zhuǎn)化為雙向循環(huán)鏈表。鏈表中的每個節(jié)點(diǎn)都有一個前驅(qū)和后繼指針。對于雙向循環(huán)鏈表,第一個節(jié)點(diǎn)的前驅(qū)是最后一個節(jié)點(diǎn),最后一個節(jié)點(diǎn)的后繼是第一個節(jié)點(diǎn)。
下圖展示了上面的二叉搜索樹轉(zhuǎn)化成的鏈表。“head” 表示指向鏈表中有最小元素的節(jié)點(diǎn)。
特別地,我們希望可以就地完成轉(zhuǎn)換操作。當(dāng)轉(zhuǎn)化完成以后,樹中節(jié)點(diǎn)的左指針需要指向前驅(qū),樹中節(jié)點(diǎn)的右指針需要指向后繼。還需要返回鏈表中的第一個節(jié)點(diǎn)的指針。
解題思路
本文解法基于性質(zhì):二叉搜索樹的中序遍歷為 遞增序列 。
將 二叉搜索樹 轉(zhuǎn)換成一個 “排序的循環(huán)雙向鏈表” ,其中包含三個要素:
- 排序鏈表:節(jié)點(diǎn)應(yīng)從小到大排序,因此應(yīng)使用 中序遍歷 “從小到大”訪問樹的節(jié)點(diǎn);
- 雙向鏈表:在構(gòu)建相鄰節(jié)點(diǎn)(設(shè)前驅(qū)節(jié)點(diǎn) pre ,當(dāng)前節(jié)點(diǎn) cur )關(guān)系時,不僅應(yīng) pre.right =cur,也應(yīng) cur.left = pre 。
- 循環(huán)鏈表:設(shè)鏈表頭節(jié)點(diǎn) head 和尾節(jié)點(diǎn) tail ,則應(yīng)構(gòu)建 head.left = tail, 和 tail.right = head 。
代碼展示
代碼如下:
class Solution {
public:
Node* treeToDoublyList(Node* root) {
if(!root) return NULL;
mid(root);
//進(jìn)行頭節(jié)點(diǎn)和尾節(jié)點(diǎn)的相互指向,這兩句的順序也是可以顛倒的
head->left=pre;
pre->right=head;
return head;
}
void mid(Node* cur)
{
if(!cur) return;
mid(cur->left);
//pre用于記錄雙向鏈表中位于cur左側(cè)的節(jié)點(diǎn),即上一次迭代中的cur,當(dāng)pre==null時,cur左側(cè)沒有節(jié)點(diǎn),即此時cur為雙向鏈表中的頭節(jié)點(diǎn)
if(pre==NULL) head=cur;
//反之,pre!=null時,cur左側(cè)存在節(jié)點(diǎn)pre,需要進(jìn)行pre.right=cur的操作。
else pre->right=cur;
//pre是否為null對這句沒有影響,且這句放在上面兩句if else之前也是可以的。
cur->left=pre;
//pre指向當(dāng)前的cur
pre=cur;
//全部迭代完成后,pre指向雙向鏈表中的尾節(jié)點(diǎn)
mid(cur->right);
}
private:
Node* head,*pre;
};
- 執(zhí)行用時:8 ms, 在所有 C++ 提交中擊敗了95.27%的用戶
- 內(nèi)存消耗:7.3 MB, 在所有 C++ 提交中擊敗了81.16%的用戶
原文鏈接:https://blog.csdn.net/qq_41884662/article/details/115435271
相關(guān)推薦
- 2022-06-02 python?面向?qū)ο箝_發(fā)及基本特征_python
- 2022-05-23 高效的數(shù)據(jù)同步工具DataX的使用及實(shí)現(xiàn)示例_數(shù)據(jù)庫其它
- 2022-04-18 uniapp中使用拷貝,復(fù)制粘貼功能,uniapp,隱藏軟鍵盤
- 2022-05-12 linq中的串聯(lián)操作符_實(shí)用技巧
- 2022-09-14 python與xml數(shù)據(jù)的交互詳解_python
- 2022-10-19 React封裝彈出框組件的方法_React
- 2022-04-26 ASP.NET?Core?MVC中Required與BindRequired用法與區(qū)別介紹_基礎(chǔ)應(yīng)用
- 2022-11-08 Golang連接并操作PostgreSQL數(shù)據(jù)庫基本操作_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支