{"id":6244,"date":"2020-02-02T10:54:12","date_gmt":"2020-02-02T18:54:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6244"},"modified":"2020-02-04T21:14:53","modified_gmt":"2020-02-05T05:14:53","slug":"leetcode-108-convert-sorted-array-to-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-108-convert-sorted-array-to-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 108. Convert Sorted Array to Binary Search Tree"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 108 Convert Sorted Array to Binary Search Tree - \u5237\u9898\u627e\u5de5\u4f5c EP306\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/O5BSAhg4n0M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an array where elements are sorted in ascending order, 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:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Given the sorted array: [-10,-3,0,5,9],\n\nOne possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:\n\n      0\n     \/ \\\n   -3   9\n   \/   \/\n -10  5<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/108-ep306.png\" alt=\"\" class=\"wp-image-6261\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/108-ep306.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/108-ep306-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/02\/108-ep306-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Recursively build a BST for a given range.<\/p>\n\n\n\n<p>def build(nums, l, r):<br>  if l &gt; r: return None<br>  m = l + (r &#8211; l) \/ 2<br>  root = TreeNode(nums[m])<br>  root.left = build(nums, l, m &#8211; 1)<br>  root.right = build(nums, m + 1, r)<br>  return root<br><br>return build(nums, 0, len(nums) &#8211; 1)<\/p>\n\n\n\n<p>Time complexity: O(n)<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* sortedArrayToBST(vector<int>& nums) {\n    function<TreeNode*(int l, int r)> build = [&](int l, int r) {\n      if (l > r) return static_cast<TreeNode*>(nullptr);      \n      int m = l + (r - l) \/ 2;\n      TreeNode* root = new TreeNode(nums[m]);\n      root->left = build(l, m - 1);\n      root->right = build(m + 1, r);\n      return root;\n    };\n    return build(0, nums.size() - 1);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"Java\">\n\/\/ Author: Huahua\nclass Solution {\n  public TreeNode sortedArrayToBST(int[] nums) {\n    return buildBST(nums, 0, nums.length - 1);\n  }\n  \n  private TreeNode buildBST(int[] nums, int l, int r) {\n    if (l > r) return null;\n    int m = l + (r - l) \/ 2;\n    TreeNode root = new TreeNode(nums[m]);\n    root.left = buildBST(nums, l, m - 1);\n    root.right = buildBST(nums, m + 1, r);\n    return root;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def sortedArrayToBST(self, nums: List[int]) -> TreeNode:\n    def buildBST(l: int, r: int) -> TreeNode:\n      if l > r: return None\n      m = l + (r - l) \/\/ 2\n      root = TreeNode(nums[m])\n      root.left = buildBST(l, m - 1)\n      root.right = buildBST(m + 1, r)\n      return root\n    return buildBST(0, len(nums) - 1)\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">JavaScript<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"javascript\">\n\/\/ Author: Huahua\nvar sortedArrayToBST = function(nums) {\n  var buildBST = function(l, r) {    \n    if (l > r) return null;\n    let m = l + Math.floor((r - l) \/ 2);\n    let root = new TreeNode(nums[m]);\n    root.left = buildBST(l, m - 1);\n    root.right = buildBST(m + 1, r);\n    return root;\n  }\n  return buildBST(0, nums.length - 1);\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[74,222,376,28],"class_list":["post-6244","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-easy","tag-on","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6244","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=6244"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6244\/revisions"}],"predecessor-version":[{"id":6262,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6244\/revisions\/6262"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}