{"id":1360,"date":"2017-12-29T21:43:15","date_gmt":"2017-12-30T05:43:15","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1360"},"modified":"2018-01-02T03:04:33","modified_gmt":"2018-01-02T11:04:33","slug":"leetcode-653-two-sum-iv-input-is-a-bst","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-653-two-sum-iv-input-is-a-bst\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 653. Two Sum IV &#8211; Input is a BST"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u8fd4\u56de\u6811\u4e2d\u662f\u5426\u5b58\u5728\u4e24\u4e2a\u8282\u70b9\u7684\u548c\u7b49\u4e8e\u7ed9\u5b9a\u7684\u76ee\u6807\u503c\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: \r\n    5\r\n   \/ \\\r\n  3   6\r\n \/ \\   \\\r\n2   4   7\r\n\r\nTarget = 9\r\n\r\nOutput: True\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: \r\n    5\r\n   \/ \\\r\n  3   6\r\n \/ \\   \\\r\n2   4   7\r\n\r\nTarget = 28\r\n\r\nOutput: False<\/pre>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 36 ms\r\nclass Solution {\r\npublic:\r\n    bool findTarget(TreeNode* root, int k) {\r\n        vector&lt;int&gt; nums;\r\n        inorder(root, nums);\r\n        int l = 0;\r\n        int r = nums.size() - 1;\r\n        while (l &lt; r) {\r\n            int sum = nums[l] + nums[r];\r\n            if (sum == k) return true;\r\n            else if (sum &lt; k)\r\n                ++l;\r\n            else\r\n                --r;\r\n        }\r\n        \r\n        return false;\r\n    }\r\nprivate:\r\n    void inorder(TreeNode* root, vector&lt;int&gt;&amp; nums) {\r\n        if (root == nullptr) return;\r\n        inorder(root-&gt;left, nums);\r\n        nums.push_back(root-&gt;val);\r\n        inorder(root-&gt;right, nums);\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u68f5\u4e8c\u53c9\u641c\u7d22\u6811\uff0c\u8fd4\u56de\u6811\u4e2d\u662f\u5426\u5b58\u5728\u4e24\u4e2a\u8282\u70b9\u7684\u548c\u7b49\u4e8e\u7ed9\u5b9a\u7684\u76ee\u6807\u503c\u3002 Problem: Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,45],"tags":[],"class_list":["post-1360","post","type-post","status-publish","format-standard","hentry","category-easy","category-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1360","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=1360"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1360\/revisions"}],"predecessor-version":[{"id":1464,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1360\/revisions\/1464"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}