Solution 1
本题是 0108. Convert Sorted Array to Binary Search Tree 的变体,直接使用上一个题的找中间位置作为根节点的分治建树思路是可以的,就是要解决链表中确定中间位置的方法。
【参考官方】找到链表中间位置的方法:快慢指针,快指针一次跳两个,慢指针一次跳一个,快指针跳到最后(或者剩一个),慢指针就是中间位置(双数情形下,是左侧的节点)。
除了中间位置的调整,边界判定条件也要跟着调整:
- 右边界是空,只最后一个节点的next
- 回溯条件为左右指针相同,此时要么是中间节点,已经上一层构造好节点,要么是右边界无效
- 分治范围因为右边界定义变化而对应变化
- 时间复杂度: O ( n log n ) O(n \log n) O(nlogn),其中 n n n为输入链表的长度,分治的对数时间复杂度,每一次找到中间位置是线性复杂度。二者相乘。
- 空间复杂度: O ( log n ) O(\log n) O(logn),其中 n n n为输入链表的长度,回溯所需的调用占用。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
return this->makeTree(head, nullptr);
}
private:
TreeNode* makeTree(ListNode* left, ListNode* right) {
if (left == right) {
return nullptr;
}
auto middle = getMiddle(left, right);
auto node = new TreeNode(middle->val);
node->left = this->makeTree(left, middle);
node->right = this->makeTree(middle->next, right);
return node;
}
ListNode* getMiddle(ListNode* left, ListNode* right) {
auto slow = left;
auto fast = left;
while (fast != right && fast->next != right) {
fast = fast->next->next;
slow = slow->next;
}
return slow; // 双数情形下,是左侧的节点
}
};
Solution 2
【参考官方】其实一个思路之前用过了但是给忘了……就是在 0106. Construct Binary Tree from Inorder and Postorder Traversal 中类似的思路,当时只是因为发现顺序遍历可以不用多传进去一个数组,使用一个索引标记一下到哪里就好了。这里实际上就可以对应为,保存一个指针,然后不断的向后移动就好了,移动的时机调整为完成最左节点的创建。
为啥没能迁移过来这个思路呢……反思反思。
而且为啥submit时间要更长呢……奇怪
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n为输入链表的长度,分治的对数时间复杂度,获取链表长度是线性复杂度。二者相加。
- 空间复杂度: O ( log n ) O(\log n) O(logn),其中 n n n为输入链表的长度,回溯所需的调用占用。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
int length = this->getlength(head);
now = head;
return this->makeTree2(0, length - 1);
}
private:
ListNode* now;
TreeNode* makeTree2(int left, int right) {
if (left > right) {
return nullptr;
}
int middle = (left + right) / 2; // 选择中间左侧的节点作为根节点
auto node = new TreeNode();
node->left = this->makeTree2(left, middle - 1);
node->val = now->val;
now = now->next;
node->right = this->makeTree2(middle + 1, right);
return node;
}
int getlength(ListNode* head) {
int length = 0;
while (head != nullptr) {
length++;
head = head->next;
}
return length;
}
};
Solution 3
Solution 1的Python实现
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
def makeTree(left: Optional[ListNode], right: Optional[ListNode]) -> Optional[TreeNode]:
if left == right:
return None
middle = getMiddle(left, right)
node = TreeNode(middle.val)
node.left = makeTree(left, middle)
node.right = makeTree(middle.next, right)
return node
def getMiddle(left: ListNode, right: ListNode) -> ListNode:
fast = left
slow = left
while fast != right and fast.next != right:
fast = fast.next.next
slow = slow.next
return slow
return makeTree(head, None)
Solution 4
Solution 2的Python实现
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
def makeTree2(left: int, right: int) -> Optional[TreeNode]:
nonlocal now
if left > right:
return None
middle = (left + right) // 2
node = TreeNode()
node.left = makeTree2(left, middle - 1)
node.val = now.val
now = now.next
node.right = makeTree2(middle + 1, right)
return node
def getLength(head: ListNode) -> int:
ans = 0
while head is not None:
ans += 1
head = head.next
return ans
length = getLength(head)
now = head
return makeTree2(0, length - 1)