{"id":6500,"date":"2020-03-15T01:39:29","date_gmt":"2020-03-15T08:39:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6500"},"modified":"2020-03-15T13:21:32","modified_gmt":"2020-03-15T20:21:32","slug":"leetcode-1382-balance-a-binary-search-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1382-balance-a-binary-search-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1382. Balance a 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 1382. Balance a Binary Search Tree - \u5237\u9898\u627e\u5de5\u4f5c EP315\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/U24USYuOWzw?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 a binary search tree, return a&nbsp;<strong>balanced<\/strong>&nbsp;binary search tree with the same node values.<\/p>\n\n\n\n<p>A binary search tree is&nbsp;<em>balanced<\/em>&nbsp;if and only if&nbsp;the depth of the two subtrees of&nbsp;every&nbsp;node never differ by more than 1.<\/p>\n\n\n\n<p>If there is more than one answer, return any of them.<\/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\/2019\/08\/22\/1515_ex1.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/08\/22\/1515_ex1_out.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,null,2,null,3,null,4,null,null]\n<strong>Output:<\/strong> [2,1,3,null,null,null,4]\n<strong>Explanation:<\/strong> This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.\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 the tree is between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>10^4<\/code>.<\/li><li>The tree nodes will have distinct values between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>10^5<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Inorder + recursion<\/strong><\/h2>\n\n\n\n<p>Use inorder traversal to collect a sorted array from BST. And then build a balanced BST from this sorted array in O(n) time.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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* balanceBST(TreeNode* root) {\n    vector<int> vals;\n    function<void(TreeNode*)> inorder = [&](TreeNode* root) {\n      if (!root) return;\n      inorder(root->left);\n      vals.push_back(root->val);\n      inorder(root->right);\n    };\n    \n    function<TreeNode*(int, int)> build = [&](int l, int r) {\n      if (l > r) return (TreeNode*)nullptr;\n      int m = l + (r - l) \/ 2;\n      auto root = new TreeNode(vals[m]);\n      root->left = build(l, m - 1);\n      root->right = build(m + 1, r);\n      return root;\n    };\n    \n    inorder(root);\n    return build(0, vals.size() - 1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary search tree, return a&nbsp;balanced&nbsp;binary search tree with the same node values. A binary search tree is&nbsp;balanced&nbsp;if and only if&nbsp;the depth of the&#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":[509,74,177,28],"class_list":["post-6500","post","type-post","status-publish","format-standard","hentry","category-tree","tag-balanced","tag-bst","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6500","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=6500"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6500\/revisions"}],"predecessor-version":[{"id":6503,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6500\/revisions\/6503"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}