{"id":8825,"date":"2021-11-27T19:51:03","date_gmt":"2021-11-28T03:51:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8825"},"modified":"2021-11-27T19:52:26","modified_gmt":"2021-11-28T03:52:26","slug":"leetcode-109-convert-sorted-list-to-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-109-convert-sorted-list-to-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 109. Convert Sorted List to Binary Search Tree"},"content":{"rendered":"\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a singly linked list where elements are&nbsp;<strong>sorted in ascending order<\/strong>, convert it to a height balanced BST.<\/p>\n\n\n\n<p>For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of&nbsp;<em>every<\/em>&nbsp;node never differ by more than 1.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/08\/17\/linked.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [-10,-3,0,5,9]\n<strong>Output:<\/strong> [0,-3,9,-10,null,5]\n<strong>Explanation:<\/strong> One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = []\n<strong>Output:<\/strong> []\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [0]\n<strong>Output:<\/strong> [0]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,3]\n<strong>Output:<\/strong> [3,1]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in&nbsp;<code>head<\/code>&nbsp;is in the range&nbsp;<code>[0, 2 * 10<sup>4<\/sup>]<\/code>.<\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= Node.val &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Recursion w\/ Fast + Slow Pointers<\/strong><\/h2>\n\n\n\n<p>For each sublist, use fast\/slow pointers to find the mid and build the tree.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(logn)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  TreeNode *sortedListToBST(ListNode *head) {\n    function<TreeNode*(ListNode*, ListNode*)> build \n      = [&](ListNode* head, ListNode* tail) -> TreeNode* {\n      if (!head || head == tail) return nullptr;\n      ListNode *fast = head, *slow = head;\n      while (fast != tail && fast->next != tail) {\n        slow = slow->next;\n        fast = fast->next->next;\n      }\n      TreeNode *root = new TreeNode(slow->val);\n      root->left = build(head, slow);\n      root->right = build(slow->next, tail);\n      return root;\n    };\n    return build(head, nullptr);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;head&nbsp;of a singly linked list where elements are&nbsp;sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[74,83,177,28],"class_list":["post-8825","post","type-post","status-publish","format-standard","hentry","category-list","tag-bst","tag-list","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8825"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8825\/revisions"}],"predecessor-version":[{"id":8827,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8825\/revisions\/8827"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}