aboutsummaryrefslogtreecommitdiff
path: root/two_sum_4.cpp
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /two_sum_4.cpp
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'two_sum_4.cpp')
-rw-r--r--two_sum_4.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/two_sum_4.cpp b/two_sum_4.cpp
new file mode 100644
index 0000000..c25bcc9
--- /dev/null
+++ b/two_sum_4.cpp
@@ -0,0 +1,24 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ bool findTarget(TreeNode *root, int k) {
+ vector<int> nums;
+ order(root, nums);
+ for (int i = 0, j = nums.size() - 1; i < j;) {
+ if (nums[i] + nums[j] == k)
+ return true;
+ (nums[i] + nums[j] < k) ? i++ : j--;
+ }
+ return false;
+ }
+
+private:
+ void order(TreeNode *root, vector<int> &nums) {
+ if (root == NULL)
+ return;
+ order(root->left, nums);
+ nums.push_back(root->val);
+ order(root->right, nums);
+ }
+};