diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md index 8ba0c6018f8ad..84abca6565357 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md @@ -56,7 +56,15 @@ tags: -### 方法一 +### 方法一:DFS + +我们先将链表转换为数组 $\textit{nums}$,然后使用深度优先搜索构造二叉搜索树。 + +我们定义一个函数 $\textit{dfs}(i, j)$,其中 $i$ 和 $j$ 表示当前区间为 $[i, j]$。每次我们选择区间中间位置 $\textit{mid}$ 的数字作为根节点,递归地构造左侧区间 $[i, \textit{mid} - 1]$ 的子树,以及右侧区间 $[\textit{mid} + 1, j]$ 的子树。最后返回 $\textit{mid}$ 对应的节点作为当前子树的根节点。 + +在主函数中,我们只需要调用 $\textit{dfs}(0, n - 1)$ 并返回即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表的长度。 @@ -75,20 +83,19 @@ tags: # self.left = left # self.right = right class Solution: - def sortedListToBST(self, head: ListNode) -> TreeNode: - def buildBST(nums, start, end): - if start > end: + def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]: + def dfs(i: int, j: int) -> Optional[TreeNode]: + if i > j: return None - mid = (start + end) >> 1 - return TreeNode( - nums[mid], buildBST(nums, start, mid - 1), buildBST(nums, mid + 1, end) - ) + mid = (i + j) >> 1 + l, r = dfs(i, mid - 1), dfs(mid + 1, j) + return TreeNode(nums[mid], l, r) nums = [] while head: nums.append(head.val) head = head.next - return buildBST(nums, 0, len(nums) - 1) + return dfs(0, len(nums) - 1) ``` #### Java @@ -120,23 +127,23 @@ class Solution: * } */ class Solution { + private List nums = new ArrayList<>(); + public TreeNode sortedListToBST(ListNode head) { - List nums = new ArrayList<>(); for (; head != null; head = head.next) { nums.add(head.val); } - return buildBST(nums, 0, nums.size() - 1); + return dfs(0, nums.size() - 1); } - private TreeNode buildBST(List nums, int start, int end) { - if (start > end) { + private TreeNode dfs(int i, int j) { + if (i > j) { return null; } - int mid = (start + end) >> 1; - TreeNode root = new TreeNode(nums.get(mid)); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + int mid = (i + j) >> 1; + TreeNode left = dfs(i, mid - 1); + TreeNode right = dfs(mid + 1, j); + return new TreeNode(nums.get(mid), left, right); } } ``` @@ -169,22 +176,19 @@ class Solution { public: TreeNode* sortedListToBST(ListNode* head) { vector nums; - for (; head != nullptr; head = head->next) { + for (; head; head = head->next) { nums.push_back(head->val); } - return buildBST(nums, 0, nums.size() - 1); - } - -private: - TreeNode* buildBST(vector& nums, int start, int end) { - if (start > end) { - return nullptr; - } - int mid = (start + end) / 2; - TreeNode* root = new TreeNode(nums[mid]); - root->left = buildBST(nums, start, mid - 1); - root->right = buildBST(nums, mid + 1, end); - return root; + auto dfs = [&](this auto&& dfs, int i, int j) -> TreeNode* { + if (i > j) { + return nullptr; + } + int mid = (i + j) >> 1; + TreeNode* left = dfs(i, mid - 1); + TreeNode* right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.size() - 1); } }; ``` @@ -209,23 +213,20 @@ private: */ func sortedListToBST(head *ListNode) *TreeNode { nums := []int{} - for head != nil { + for ; head != nil; head = head.Next { nums = append(nums, head.Val) - head = head.Next - } - return buildBST(nums, 0, len(nums)-1) -} - -func buildBST(nums []int, start, end int) *TreeNode { - if start > end { - return nil } - mid := (start + end) >> 1 - return &TreeNode{ - Val: nums[mid], - Left: buildBST(nums, start, mid-1), - Right: buildBST(nums, mid+1, end), + var dfs func(i, j int) *TreeNode + dfs = func(i, j int) *TreeNode { + if i > j { + return nil + } + mid := (i + j) >> 1 + left := dfs(i, mid-1) + right := dfs(mid+1, j) + return &TreeNode{nums[mid], left, right} } + return dfs(0, len(nums)-1) } ``` @@ -258,26 +259,21 @@ func buildBST(nums []int, start, end int) *TreeNode { * } */ -const find = (start: ListNode | null, end: ListNode | null) => { - let fast = start; - let slow = start; - while (fast !== end && fast.next !== end) { - fast = fast.next.next; - slow = slow.next; - } - return slow; -}; - -const build = (start: ListNode | null, end: ListNode | null) => { - if (start == end) { - return null; - } - const node = find(start, end); - return new TreeNode(node.val, build(start, node), build(node.next, end)); -}; - function sortedListToBST(head: ListNode | null): TreeNode | null { - return build(head, null); + const nums: number[] = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i: number, j: number): TreeNode | null => { + if (i > j) { + return null; + } + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.length - 1); } ``` @@ -320,27 +316,29 @@ function sortedListToBST(head: ListNode | null): TreeNode | null { // } use std::cell::RefCell; use std::rc::Rc; + impl Solution { - fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if start == end { - return None; + pub fn sorted_list_to_bst(head: Option>) -> Option>> { + let mut nums = Vec::new(); + let mut current = head; + while let Some(node) = current { + nums.push(node.val); + current = node.next; } - let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) - } - pub fn sorted_list_to_bst(head: Option>) -> Option>> { - let mut vals = Vec::new(); - let mut cur = &head; - while let Some(node) = cur { - vals.push(node.val); - cur = &node.next; + fn dfs(nums: &[i32]) -> Option>> { + if nums.is_empty() { + return None; + } + let mid = nums.len() / 2; + Some(Rc::new(RefCell::new(TreeNode { + val: nums[mid], + left: dfs(&nums[..mid]), + right: dfs(&nums[mid + 1..]), + }))) } - Self::build(&vals, 0, vals.len()) + + dfs(&nums) } } ``` @@ -368,22 +366,20 @@ impl Solution { * @return {TreeNode} */ var sortedListToBST = function (head) { - const buildBST = (nums, start, end) => { - if (start > end) { + const nums = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i, j) => { + if (i > j) { return null; } - const mid = (start + end) >> 1; - const root = new TreeNode(nums[mid]); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); }; - - const nums = new Array(); - for (; head != null; head = head.next) { - nums.push(head.val); - } - return buildBST(nums, 0, nums.length - 1); + return dfs(0, nums.length - 1); }; ``` @@ -405,30 +401,38 @@ var sortedListToBST = function (head) { * struct TreeNode *right; * }; */ -struct ListNode* find(struct ListNode* start, struct ListNode* end) { - struct ListNode* fast = start; - struct ListNode* slow = start; - while (fast != end && fast->next != end) { - fast = fast->next->next; - slow = slow->next; - } - return slow; -} - -struct TreeNode* bulid(struct ListNode* start, struct ListNode* end) { - if (start == end) { +struct TreeNode* dfs(int* nums, int i, int j) { + if (i > j) { return NULL; } - struct ListNode* node = find(start, end); - struct TreeNode* ans = malloc(sizeof(struct TreeNode)); - ans->val = node->val; - ans->left = bulid(start, node); - ans->right = bulid(node->next, end); - return ans; + int mid = (i + j) >> 1; + struct TreeNode* left = dfs(nums, i, mid - 1); + struct TreeNode* right = dfs(nums, mid + 1, j); + struct TreeNode* root = (struct TreeNode*) malloc(sizeof(struct TreeNode)); + root->val = nums[mid]; + root->left = left; + root->right = right; + return root; } struct TreeNode* sortedListToBST(struct ListNode* head) { - return bulid(head, NULL); + int size = 0; + struct ListNode* temp = head; + while (temp) { + size++; + temp = temp->next; + } + + int* nums = (int*) malloc(size * sizeof(int)); + temp = head; + for (int i = 0; i < size; i++) { + nums[i] = temp->val; + temp = temp->next; + } + + struct TreeNode* root = dfs(nums, 0, size - 1); + free(nums); + return root; } ``` diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md index 79f70e47c211b..fff3994332340 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md @@ -52,7 +52,15 @@ tags: -### Solution 1 +### Solution 1: DFS + +We first convert the linked list to an array $\textit{nums}$, and then use depth-first search to construct the binary search tree. + +We define a function $\textit{dfs}(i, j)$, where $i$ and $j$ represent the current interval $[i, j]$. Each time, we choose the number at the middle position $\textit{mid}$ of the interval as the root node, recursively construct the left subtree for the interval $[i, \textit{mid} - 1]$, and the right subtree for the interval $[\textit{mid} + 1, j]$. Finally, we return the node corresponding to $\textit{mid}$ as the root node of the current subtree. + +In the main function, we just need to call $\textit{dfs}(0, n - 1)$ and return the result. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the linked list. @@ -71,20 +79,19 @@ tags: # self.left = left # self.right = right class Solution: - def sortedListToBST(self, head: ListNode) -> TreeNode: - def buildBST(nums, start, end): - if start > end: + def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]: + def dfs(i: int, j: int) -> Optional[TreeNode]: + if i > j: return None - mid = (start + end) >> 1 - return TreeNode( - nums[mid], buildBST(nums, start, mid - 1), buildBST(nums, mid + 1, end) - ) + mid = (i + j) >> 1 + l, r = dfs(i, mid - 1), dfs(mid + 1, j) + return TreeNode(nums[mid], l, r) nums = [] while head: nums.append(head.val) head = head.next - return buildBST(nums, 0, len(nums) - 1) + return dfs(0, len(nums) - 1) ``` #### Java @@ -116,23 +123,23 @@ class Solution: * } */ class Solution { + private List nums = new ArrayList<>(); + public TreeNode sortedListToBST(ListNode head) { - List nums = new ArrayList<>(); for (; head != null; head = head.next) { nums.add(head.val); } - return buildBST(nums, 0, nums.size() - 1); + return dfs(0, nums.size() - 1); } - private TreeNode buildBST(List nums, int start, int end) { - if (start > end) { + private TreeNode dfs(int i, int j) { + if (i > j) { return null; } - int mid = (start + end) >> 1; - TreeNode root = new TreeNode(nums.get(mid)); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + int mid = (i + j) >> 1; + TreeNode left = dfs(i, mid - 1); + TreeNode right = dfs(mid + 1, j); + return new TreeNode(nums.get(mid), left, right); } } ``` @@ -165,22 +172,19 @@ class Solution { public: TreeNode* sortedListToBST(ListNode* head) { vector nums; - for (; head != nullptr; head = head->next) { + for (; head; head = head->next) { nums.push_back(head->val); } - return buildBST(nums, 0, nums.size() - 1); - } - -private: - TreeNode* buildBST(vector& nums, int start, int end) { - if (start > end) { - return nullptr; - } - int mid = (start + end) / 2; - TreeNode* root = new TreeNode(nums[mid]); - root->left = buildBST(nums, start, mid - 1); - root->right = buildBST(nums, mid + 1, end); - return root; + auto dfs = [&](this auto&& dfs, int i, int j) -> TreeNode* { + if (i > j) { + return nullptr; + } + int mid = (i + j) >> 1; + TreeNode* left = dfs(i, mid - 1); + TreeNode* right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.size() - 1); } }; ``` @@ -205,23 +209,20 @@ private: */ func sortedListToBST(head *ListNode) *TreeNode { nums := []int{} - for head != nil { + for ; head != nil; head = head.Next { nums = append(nums, head.Val) - head = head.Next - } - return buildBST(nums, 0, len(nums)-1) -} - -func buildBST(nums []int, start, end int) *TreeNode { - if start > end { - return nil } - mid := (start + end) >> 1 - return &TreeNode{ - Val: nums[mid], - Left: buildBST(nums, start, mid-1), - Right: buildBST(nums, mid+1, end), + var dfs func(i, j int) *TreeNode + dfs = func(i, j int) *TreeNode { + if i > j { + return nil + } + mid := (i + j) >> 1 + left := dfs(i, mid-1) + right := dfs(mid+1, j) + return &TreeNode{nums[mid], left, right} } + return dfs(0, len(nums)-1) } ``` @@ -254,26 +255,21 @@ func buildBST(nums []int, start, end int) *TreeNode { * } */ -const find = (start: ListNode | null, end: ListNode | null) => { - let fast = start; - let slow = start; - while (fast !== end && fast.next !== end) { - fast = fast.next.next; - slow = slow.next; - } - return slow; -}; - -const build = (start: ListNode | null, end: ListNode | null) => { - if (start == end) { - return null; - } - const node = find(start, end); - return new TreeNode(node.val, build(start, node), build(node.next, end)); -}; - function sortedListToBST(head: ListNode | null): TreeNode | null { - return build(head, null); + const nums: number[] = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i: number, j: number): TreeNode | null => { + if (i > j) { + return null; + } + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.length - 1); } ``` @@ -316,27 +312,29 @@ function sortedListToBST(head: ListNode | null): TreeNode | null { // } use std::cell::RefCell; use std::rc::Rc; + impl Solution { - fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if start == end { - return None; + pub fn sorted_list_to_bst(head: Option>) -> Option>> { + let mut nums = Vec::new(); + let mut current = head; + while let Some(node) = current { + nums.push(node.val); + current = node.next; } - let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) - } - pub fn sorted_list_to_bst(head: Option>) -> Option>> { - let mut vals = Vec::new(); - let mut cur = &head; - while let Some(node) = cur { - vals.push(node.val); - cur = &node.next; + fn dfs(nums: &[i32]) -> Option>> { + if nums.is_empty() { + return None; + } + let mid = nums.len() / 2; + Some(Rc::new(RefCell::new(TreeNode { + val: nums[mid], + left: dfs(&nums[..mid]), + right: dfs(&nums[mid + 1..]), + }))) } - Self::build(&vals, 0, vals.len()) + + dfs(&nums) } } ``` @@ -364,22 +362,20 @@ impl Solution { * @return {TreeNode} */ var sortedListToBST = function (head) { - const buildBST = (nums, start, end) => { - if (start > end) { + const nums = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i, j) => { + if (i > j) { return null; } - const mid = (start + end) >> 1; - const root = new TreeNode(nums[mid]); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); }; - - const nums = new Array(); - for (; head != null; head = head.next) { - nums.push(head.val); - } - return buildBST(nums, 0, nums.length - 1); + return dfs(0, nums.length - 1); }; ``` @@ -401,30 +397,38 @@ var sortedListToBST = function (head) { * struct TreeNode *right; * }; */ -struct ListNode* find(struct ListNode* start, struct ListNode* end) { - struct ListNode* fast = start; - struct ListNode* slow = start; - while (fast != end && fast->next != end) { - fast = fast->next->next; - slow = slow->next; - } - return slow; -} - -struct TreeNode* bulid(struct ListNode* start, struct ListNode* end) { - if (start == end) { +struct TreeNode* dfs(int* nums, int i, int j) { + if (i > j) { return NULL; } - struct ListNode* node = find(start, end); - struct TreeNode* ans = malloc(sizeof(struct TreeNode)); - ans->val = node->val; - ans->left = bulid(start, node); - ans->right = bulid(node->next, end); - return ans; + int mid = (i + j) >> 1; + struct TreeNode* left = dfs(nums, i, mid - 1); + struct TreeNode* right = dfs(nums, mid + 1, j); + struct TreeNode* root = (struct TreeNode*) malloc(sizeof(struct TreeNode)); + root->val = nums[mid]; + root->left = left; + root->right = right; + return root; } struct TreeNode* sortedListToBST(struct ListNode* head) { - return bulid(head, NULL); + int size = 0; + struct ListNode* temp = head; + while (temp) { + size++; + temp = temp->next; + } + + int* nums = (int*) malloc(size * sizeof(int)); + temp = head; + for (int i = 0; i < size; i++) { + nums[i] = temp->val; + temp = temp->next; + } + + struct TreeNode* root = dfs(nums, 0, size - 1); + free(nums); + return root; } ``` diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.c b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.c index f32e42d04329e..bfeda09b4b167 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.c +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.c @@ -13,28 +13,36 @@ * struct TreeNode *right; * }; */ -struct ListNode* find(struct ListNode* start, struct ListNode* end) { - struct ListNode* fast = start; - struct ListNode* slow = start; - while (fast != end && fast->next != end) { - fast = fast->next->next; - slow = slow->next; - } - return slow; -} - -struct TreeNode* bulid(struct ListNode* start, struct ListNode* end) { - if (start == end) { +struct TreeNode* dfs(int* nums, int i, int j) { + if (i > j) { return NULL; } - struct ListNode* node = find(start, end); - struct TreeNode* ans = malloc(sizeof(struct TreeNode)); - ans->val = node->val; - ans->left = bulid(start, node); - ans->right = bulid(node->next, end); - return ans; + int mid = (i + j) >> 1; + struct TreeNode* left = dfs(nums, i, mid - 1); + struct TreeNode* right = dfs(nums, mid + 1, j); + struct TreeNode* root = (struct TreeNode*) malloc(sizeof(struct TreeNode)); + root->val = nums[mid]; + root->left = left; + root->right = right; + return root; } struct TreeNode* sortedListToBST(struct ListNode* head) { - return bulid(head, NULL); -} \ No newline at end of file + int size = 0; + struct ListNode* temp = head; + while (temp) { + size++; + temp = temp->next; + } + + int* nums = (int*) malloc(size * sizeof(int)); + temp = head; + for (int i = 0; i < size; i++) { + nums[i] = temp->val; + temp = temp->next; + } + + struct TreeNode* root = dfs(nums, 0, size - 1); + free(nums); + return root; +} diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp index ee5cfcc48ece5..1eb0fe703542a 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp @@ -23,21 +23,18 @@ class Solution { public: TreeNode* sortedListToBST(ListNode* head) { vector nums; - for (; head != nullptr; head = head->next) { + for (; head; head = head->next) { nums.push_back(head->val); } - return buildBST(nums, 0, nums.size() - 1); + auto dfs = [&](this auto&& dfs, int i, int j) -> TreeNode* { + if (i > j) { + return nullptr; + } + int mid = (i + j) >> 1; + TreeNode* left = dfs(i, mid - 1); + TreeNode* right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.size() - 1); } - -private: - TreeNode* buildBST(vector& nums, int start, int end) { - if (start > end) { - return nullptr; - } - int mid = (start + end) / 2; - TreeNode* root = new TreeNode(nums[mid]); - root->left = buildBST(nums, start, mid - 1); - root->right = buildBST(nums, mid + 1, end); - return root; - } -}; \ No newline at end of file +}; diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.go b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.go index b66788fc49a2f..6552a289a76a1 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.go +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.go @@ -15,21 +15,18 @@ */ func sortedListToBST(head *ListNode) *TreeNode { nums := []int{} - for head != nil { + for ; head != nil; head = head.Next { nums = append(nums, head.Val) - head = head.Next } - return buildBST(nums, 0, len(nums)-1) -} - -func buildBST(nums []int, start, end int) *TreeNode { - if start > end { - return nil - } - mid := (start + end) >> 1 - return &TreeNode{ - Val: nums[mid], - Left: buildBST(nums, start, mid-1), - Right: buildBST(nums, mid+1, end), + var dfs func(i, j int) *TreeNode + dfs = func(i, j int) *TreeNode { + if i > j { + return nil + } + mid := (i + j) >> 1 + left := dfs(i, mid-1) + right := dfs(mid+1, j) + return &TreeNode{nums[mid], left, right} } -} \ No newline at end of file + return dfs(0, len(nums)-1) +} diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.java b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.java index 1f721b782aeaf..8f48b4cc51a15 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.java +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.java @@ -24,22 +24,22 @@ * } */ class Solution { + private List nums = new ArrayList<>(); + public TreeNode sortedListToBST(ListNode head) { - List nums = new ArrayList<>(); for (; head != null; head = head.next) { nums.add(head.val); } - return buildBST(nums, 0, nums.size() - 1); + return dfs(0, nums.size() - 1); } - private TreeNode buildBST(List nums, int start, int end) { - if (start > end) { + private TreeNode dfs(int i, int j) { + if (i > j) { return null; } - int mid = (start + end) >> 1; - TreeNode root = new TreeNode(nums.get(mid)); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + int mid = (i + j) >> 1; + TreeNode left = dfs(i, mid - 1); + TreeNode right = dfs(mid + 1, j); + return new TreeNode(nums.get(mid), left, right); } -} \ No newline at end of file +} diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.js b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.js index f8306a421e369..dc5f86343f7db 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.js +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.js @@ -18,20 +18,18 @@ * @return {TreeNode} */ var sortedListToBST = function (head) { - const buildBST = (nums, start, end) => { - if (start > end) { + const nums = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i, j) => { + if (i > j) { return null; } - const mid = (start + end) >> 1; - const root = new TreeNode(nums[mid]); - root.left = buildBST(nums, start, mid - 1); - root.right = buildBST(nums, mid + 1, end); - return root; + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); }; - - const nums = new Array(); - for (; head != null; head = head.next) { - nums.push(head.val); - } - return buildBST(nums, 0, nums.length - 1); + return dfs(0, nums.length - 1); }; diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.py b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.py index 3afda8b8cc901..a1dc40741c84c 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.py +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.py @@ -10,17 +10,16 @@ # self.left = left # self.right = right class Solution: - def sortedListToBST(self, head: ListNode) -> TreeNode: - def buildBST(nums, start, end): - if start > end: + def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]: + def dfs(i: int, j: int) -> Optional[TreeNode]: + if i > j: return None - mid = (start + end) >> 1 - return TreeNode( - nums[mid], buildBST(nums, start, mid - 1), buildBST(nums, mid + 1, end) - ) + mid = (i + j) >> 1 + l, r = dfs(i, mid - 1), dfs(mid + 1, j) + return TreeNode(nums[mid], l, r) nums = [] while head: nums.append(head.val) head = head.next - return buildBST(nums, 0, len(nums) - 1) + return dfs(0, len(nums) - 1) diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs index 2d96335ac0ffb..acb7860a69bf0 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.rs @@ -34,26 +34,28 @@ // } use std::cell::RefCell; use std::rc::Rc; + impl Solution { - fn build(vals: &Vec, start: usize, end: usize) -> Option>> { - if start == end { - return None; + pub fn sorted_list_to_bst(head: Option>) -> Option>> { + let mut nums = Vec::new(); + let mut current = head; + while let Some(node) = current { + nums.push(node.val); + current = node.next; } - let mid = (start + end) >> 1; - Some(Rc::new(RefCell::new(TreeNode { - val: vals[mid], - left: Self::build(vals, start, mid), - right: Self::build(vals, mid + 1, end), - }))) - } - pub fn sorted_list_to_bst(head: Option>) -> Option>> { - let mut vals = Vec::new(); - let mut cur = &head; - while let Some(node) = cur { - vals.push(node.val); - cur = &node.next; + fn dfs(nums: &[i32]) -> Option>> { + if nums.is_empty() { + return None; + } + let mid = nums.len() / 2; + Some(Rc::new(RefCell::new(TreeNode { + val: nums[mid], + left: dfs(&nums[..mid]), + right: dfs(&nums[mid + 1..]), + }))) } - Self::build(&vals, 0, vals.len()) + + dfs(&nums) } } diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.ts b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.ts index 8f9a88f34b619..3c4e72f7c6099 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.ts +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.ts @@ -24,24 +24,19 @@ * } */ -const find = (start: ListNode | null, end: ListNode | null) => { - let fast = start; - let slow = start; - while (fast !== end && fast.next !== end) { - fast = fast.next.next; - slow = slow.next; - } - return slow; -}; - -const build = (start: ListNode | null, end: ListNode | null) => { - if (start == end) { - return null; - } - const node = find(start, end); - return new TreeNode(node.val, build(start, node), build(node.next, end)); -}; - function sortedListToBST(head: ListNode | null): TreeNode | null { - return build(head, null); + const nums: number[] = []; + for (; head; head = head.next) { + nums.push(head.val); + } + const dfs = (i: number, j: number): TreeNode | null => { + if (i > j) { + return null; + } + const mid = (i + j) >> 1; + const left = dfs(i, mid - 1); + const right = dfs(mid + 1, j); + return new TreeNode(nums[mid], left, right); + }; + return dfs(0, nums.length - 1); }