{"id":4285,"date":"2018-11-10T23:41:47","date_gmt":"2018-11-11T07:41:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4285"},"modified":"2018-11-10T23:43:54","modified_gmt":"2018-11-11T07:43:54","slug":"leetcode-938-range-sum-of-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-938-range-sum-of-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 938. Range Sum of BST"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given the\u00a0<code>root<\/code>\u00a0node of a binary search tree, return the sum of values of all nodes with value between\u00a0<code>L<\/code>\u00a0and\u00a0<code>R<\/code>\u00a0(inclusive).<\/p>\n<p>The binary search tree is guaranteed to have unique values.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>root = <span id=\"example-input-1-1\">[10,5,15,3,7,null,18]<\/span>, L = <span id=\"example-input-1-2\">7<\/span>, R = <span id=\"example-input-1-3\">15<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">32<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>root = <span id=\"example-input-2-1\">[10,5,15,3,7,13,18,1,null,6]<\/span>, L = <span id=\"example-input-2-2\">6<\/span>, R = <span id=\"example-input-2-3\">10<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">23<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li>The number of nodes in the tree is at most\u00a0<code>10000<\/code>.<\/li>\n<li>The final answer is guaranteed to be less than\u00a0<code>2^31<\/code>.<\/li>\n<\/ol>\n<h1>Solution: In-order traversal<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, running time: 72 ms\r\nclass Solution {\r\npublic:\r\n  int rangeSumBST(TreeNode* root, int L, int R) {\r\n    if (!root) return 0;\r\n    int sum = 0;\r\n    if (root-&gt;val &gt;= L) sum += rangeSumBST(root-&gt;left, L, R);\r\n    if (root-&gt;val &gt;= L &amp;&amp; root-&gt;val &lt;= R) sum += root-&gt;val;\r\n    if (root-&gt;val &lt;= R) sum += rangeSumBST(root-&gt;right, L, R);\r\n    return sum;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given the\u00a0root\u00a0node of a binary search tree, return the sum of values of all nodes with value between\u00a0L\u00a0and\u00a0R\u00a0(inclusive). The binary search tree is guaranteed&#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,58,139,201],"class_list":["post-4285","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-inorder","tag-range","tag-range-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4285","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=4285"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4285\/revisions"}],"predecessor-version":[{"id":4289,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4285\/revisions\/4289"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}