C++實現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)
[LeetCode] 109.Convert Sorted List to Binary Search Tree 將有序鏈表轉(zhuǎn)為二叉搜索樹
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
這道題是要求把有序鏈表轉(zhuǎn)為二叉搜索樹,和之前那道 Convert Sorted Array to Binary Search Tree 思路完全一樣,只不過是操作的數(shù)據(jù)類型有所差別,一個是數(shù)組,一個是鏈表。數(shù)組方便就方便在可以通過index直接訪問任意一個元素,而鏈表不行。由于二分查找法每次需要找到中點,而鏈表的查找中間點可以通過快慢指針來操作,可參見之前的兩篇博客 Reorder List 和 Linked List Cycle II 有關(guān)快慢指針的應(yīng)用。找到中點后,要以中點的值建立一個數(shù)的根節(jié)點,然后需要把原鏈表斷開,分為前后兩個鏈表,都不能包含原中節(jié)點,然后再分別對這兩個鏈表遞歸調(diào)用原函數(shù),分別連上左右子節(jié)點即可。代碼如下:
解法一:
class Solution {
public:
TreeNode *sortedListToBST(ListNode* head) {
if (!head) return NULL;
if (!head->next) return new TreeNode(head->val);
ListNode *slow = head, *fast = head, *last = slow;
while (fast->next && fast->next->next) {
last = slow;
slow = slow->next;
fast = fast->next->next;
}
fast = slow->next;
last->next = NULL;
TreeNode *cur = new TreeNode(slow->val);
if (head != slow) cur->left = sortedListToBST(head);
cur->right = sortedListToBST(fast);
return cur;
}
};
我們也可以采用如下的遞歸方法,重寫一個遞歸函數(shù),有兩個輸入?yún)?shù),子鏈表的起點和終點,因為知道了這兩個點,鏈表的范圍就可以確定了,而直接將中間部分轉(zhuǎn)換為二叉搜索樹即可,遞歸函數(shù)中的內(nèi)容跟上面解法中的極其相似,參見代碼如下:
解法二:
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if (!head) return NULL;
return helper(head, NULL);
}
TreeNode* helper(ListNode* head, ListNode* tail) {
if (head == tail) return NULL;
ListNode *slow = head, *fast = head;
while (fast != tail && fast->next != tail) {
slow = slow->next;
fast = fast->next->next;
}
TreeNode *cur = new TreeNode(slow->val);
cur->left = helper(head, slow);
cur->right = helper(slow->next, tail);
return cur;
}
};
類似題目:
Convert Sorted Array to Binary Search Tree
參考資料:
https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
到此這篇關(guān)于C++實現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)的文章就介紹到這了,更多相關(guān)C++實現(xiàn)將有序鏈表轉(zhuǎn)為二叉搜索樹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++數(shù)據(jù)結(jié)構(gòu)二叉搜索樹的實現(xiàn)應(yīng)用與分析
- C++實現(xiàn)LeetCode(173.二叉搜索樹迭代器)
- C++實現(xiàn)LeetCode(108.將有序數(shù)組轉(zhuǎn)為二叉搜索樹)
- C++實現(xiàn)LeetCode(99.復原二叉搜索樹)
- C++實現(xiàn)LeetCode(98.驗證二叉搜索樹)
- C++實現(xiàn)LeetCode(96.獨一無二的二叉搜索樹)
- C++實現(xiàn)LeetCode(95.獨一無二的二叉搜索樹之二)
- C++ 二叉搜索樹(BST)的實現(xiàn)方法
- C++ 超詳細快速掌握二叉搜索樹
相關(guān)文章
C/C++寬窄字符轉(zhuǎn)換與輸出的多種實現(xiàn)方法
本文主要介紹了C/C++寬窄字符轉(zhuǎn)換與輸出的多種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
C語言中函數(shù)棧幀的創(chuàng)建和銷毀的深層分析
在C語言中,每一個正在運行的函數(shù)都有一個棧幀與其對應(yīng),棧幀中存儲的是該函數(shù)的返回地址和局部變量。從邏輯上講,棧幀就是一個函數(shù)執(zhí)行的環(huán)境:函數(shù)參數(shù)、函數(shù)的局部變量、函數(shù)執(zhí)行完后返回到哪里等等2022-04-04
java 中ArrayList與LinkedList性能比較
這篇文章主要介紹了java 中ArrayList與LinkedList性能比較的相關(guān)資料,需要的朋友可以參考下2017-03-03
設(shè)計模式中的備忘錄模式解析及相關(guān)C++實例應(yīng)用
這篇文章主要介紹了設(shè)計模式中的備忘錄模式解析及相關(guān)C++實例應(yīng)用,備忘錄模式也經(jīng)常被用來在命令模式中維護可以撤銷(Undo)操作的狀態(tài),需要的朋友可以參考下2016-03-03
關(guān)于C++中定義比較函數(shù)的三種方法小結(jié)
下面小編就為大家?guī)硪黄P(guān)于C++中定義比較函數(shù)的三種方法小結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10

