{"id":5617,"date":"2019-09-29T20:44:30","date_gmt":"2019-09-30T03:44:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5617"},"modified":"2019-09-29T20:44:43","modified_gmt":"2019-09-30T03:44:43","slug":"leetcode-96-unique-binary-search-trees","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-96-unique-binary-search-trees\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 96. Unique Binary Search Trees"},"content":{"rendered":"\n<p>Given&nbsp;<em>n<\/em>, how many structurally unique&nbsp;<strong>BST&#8217;s<\/strong>&nbsp;(binary search trees) that store values 1 &#8230;&nbsp;<em>n<\/em>?<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> 3\n<strong>Output:<\/strong> 5\n<strong>Explanation:\n<\/strong>Given <em>n<\/em> = 3, there are a total of 5 unique BST's:\n\n   1         3     3      2      1\n    \\       \/     \/      \/ \\      \\\n     3     2     1      1   3      2\n    \/     \/       \\                 \\\n   2     1         2                 3<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>dp[i] = sum(dp[j] * dp[i &#8211; j &#8211; 1]) (0 &lt;= j &lt; i )<br> <br>root: 1 node <br>left child: j nodes<br>right child i &#8211; j &#8211; 1 nodes<br><br>try all possible partitions<\/p>\n\n\n\n<p>ans = dp[n]<\/p>\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\nclass Solution {\npublic:\n  int numTrees(int n) {\n    vector<int> dp(n + 1);\n    dp[0] = 1;\n    for (int i = 1; i <= n; i++)\n      for(int j = 0; j < i; j++)\n        dp[i] += dp[j] * dp[i - j - 1];\n    return dp[n];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given&nbsp;n, how many structurally unique&nbsp;BST&#8217;s&nbsp;(binary search trees) that store values 1 &#8230;&nbsp;n? Example: Input: 3 Output: 5 Explanation: Given n = 3, there are a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[8,18,28],"class_list":["post-5617","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-counting","tag-dp","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5617","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=5617"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5617\/revisions"}],"predecessor-version":[{"id":5619,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5617\/revisions\/5619"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}