{"id":4966,"date":"2019-03-09T22:08:55","date_gmt":"2019-03-10T06:08:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4966"},"modified":"2019-03-09T22:55:47","modified_gmt":"2019-03-10T06:55:47","slug":"leetcode-1008-construct-binary-search-tree-from-preorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1008-construct-binary-search-tree-from-preorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1008. Construct Binary Search Tree from Preorder Traversal"},"content":{"rendered":"\n<p>Return the root node of a binary&nbsp;<strong>search<\/strong>&nbsp;tree that matches the given&nbsp;<code>preorder<\/code>&nbsp;traversal.<\/p>\n\n\n\n<p><em>(Recall that a binary search tree&nbsp;is a binary tree where for every&nbsp;node, any descendant of&nbsp;<code>node.left<\/code>&nbsp;has a value&nbsp;<code>&lt;<\/code>&nbsp;<code>node.val<\/code>, and any descendant of&nbsp;<code>node.right<\/code>&nbsp;has a value&nbsp;<code>&gt;<\/code>&nbsp;<code>node.val<\/code>.&nbsp; Also recall that a preorder traversal&nbsp;displays the value of the&nbsp;<code>node<\/code>&nbsp;first, then traverses&nbsp;<code>node.left<\/code>, then traverses&nbsp;<code>node.right<\/code>.)<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>[8,5,1,7,10,12]\n<strong>Output: <\/strong>[8,5,10,1,7,null,12]\n\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= preorder.length &lt;= 100<\/code><\/li><li>The values of&nbsp;<code>preorder<\/code>&nbsp;are distinct.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^2)<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, 8 ms, 10.8 MB\nclass Solution {\npublic:\n  TreeNode* bstFromPreorder(vector<int>& preorder) {\n    return build(preorder, 0, preorder.size());\n  }\nprivate:\n  TreeNode* build(const vector<int>& preorder, int s, int e) {\n    if (s >= e) return nullptr;\n    auto root = new TreeNode(preorder[s]);\n    int m = s;\n    while (m < e &#038;&#038; preorder[m] <= root->val) ++m;\n    root->left = build(preorder, s + 1, m);\n    root->right = build(preorder, m, e);\n    return root;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/iterator<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  TreeNode* bstFromPreorder(vector<int>& preorder) {\n    return build(begin(preorder), end(preorder));\n  }\nprivate:\n  typedef vector<int>::const_iterator IT;\n  TreeNode* build(IT s, IT e) {\n    if (s >= e) return nullptr;\n    auto root = new TreeNode(*s);\n    IT m = s;\n    while (m < e &#038;&#038; *m <= *s) ++m;\n    root->left = build(s + 1, m);\n    root->right = build(m, e);\n    return root;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Return the root node of a binary&nbsp;search&nbsp;tree that matches the given&nbsp;preorder&nbsp;traversal. (Recall that a binary search tree&nbsp;is a binary tree where for every&nbsp;node, any descendant&#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":[457,28],"class_list":["post-4966","post","type-post","status-publish","format-standard","hentry","category-tree","tag-construct","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4966","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=4966"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions"}],"predecessor-version":[{"id":4969,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4966\/revisions\/4969"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}