{"id":2274,"date":"2018-03-21T21:19:55","date_gmt":"2018-03-22T04:19:55","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2274"},"modified":"2018-03-21T21:26:52","modified_gmt":"2018-03-22T04:26:52","slug":"leetcode-538-convert-bst-to-greater-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-538-convert-bst-to-greater-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 538. Convert BST to Greater Tree"},"content":{"rendered":"<div class=\"question-description\">\n<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u628a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u52a0\u4e0a\u6bd4\u4ed6\u5927\u7684\u8282\u70b9\u7684\u548c\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/convert-bst-to-greater-tree\/description\/\">https:\/\/leetcode.com\/problems\/convert-bst-to-greater-tree\/description\/<\/a><\/p>\n<p>Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b> The root of a Binary Search Tree like this:\r\n              5\r\n            \/   \\\r\n           2     13\r\n\r\n<b>Output:<\/b> The root of a Greater Tree like this:\r\n             18\r\n            \/   \\\r\n          20     13\r\n<\/pre>\n<\/div>\n<h1>Solution: reversed inorder traversal<\/h1>\n<p>in a BST, we can visit every node in the decreasing order. Using a member sum to track the sum of all visited nodes.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 56 ms\r\nclass Solution {\r\npublic:\r\n  TreeNode* convertBST(TreeNode* root) {\r\n    sum = 0;\r\n    inorder(root);\r\n    return root;\r\n  }\r\nprivate:\r\n  int sum;\r\n  void rinorder(TreeNode* root) {\r\n    if (root == nullptr) return;\r\n    rinorder(root-&gt;right);\r\n    root-&gt;val = (sum += root-&gt;val);\r\n    rinorder(root-&gt;left);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u628a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6bcf\u4e2a\u8282\u70b9\u52a0\u4e0a\u6bd4\u4ed6\u5927\u7684\u8282\u70b9\u7684\u548c\u3002 https:\/\/leetcode.com\/problems\/convert-bst-to-greater-tree\/description\/ Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed&#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,58],"class_list":["post-2274","post","type-post","status-publish","format-standard","hentry","category-tree","tag-bst","tag-easy","tag-inorder","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2274","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=2274"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2274\/revisions"}],"predecessor-version":[{"id":2278,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2274\/revisions\/2278"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}