From 3f44f88595a6dc3cf4b59a8ae529ae3a91dff701 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Thu, 16 Apr 2026 10:16:57 +0900 Subject: [PATCH] add files --- 0112-path-sum/memo.md | 11 +++++++++++ 0112-path-sum/step1.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 0112-path-sum/step2.cpp | 26 ++++++++++++++++++++++++++ 0112-path-sum/step3.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 0112-path-sum/memo.md create mode 100644 0112-path-sum/step1.cpp create mode 100644 0112-path-sum/step2.cpp create mode 100644 0112-path-sum/step3.cpp diff --git a/0112-path-sum/memo.md b/0112-path-sum/memo.md new file mode 100644 index 0000000..a85d02e --- /dev/null +++ b/0112-path-sum/memo.md @@ -0,0 +1,11 @@ +### step1 + +最初にflagをfalseでもっておいて、DFSしながら条件に合うものがあればflagをtrueにするというやり方で実装。 + +### step2 + +ChatGPTにflagをもたないやり方に改善してもらった。 + +### step3 + +3回通すまで書き直し。 diff --git a/0112-path-sum/step1.cpp b/0112-path-sum/step1.cpp new file mode 100644 index 0000000..d97a3ac --- /dev/null +++ b/0112-path-sum/step1.cpp @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int flag = false; + + void scan(TreeNode* root, int sum, int targetSum) { + if (root == nullptr) { + return; + } + if (root->left == nullptr && root->right == nullptr) { + flag = flag || (sum == targetSum); + return; + } + if (root->left != nullptr) { + scan(root->left, sum + root->left->val, targetSum); + } + if (root->right != nullptr) { + scan(root->right, sum + root->right->val, targetSum); + } + } + + bool hasPathSum(TreeNode* root, int targetSum) { + if (root == nullptr) { + return false; + } + scan(root, root->val, targetSum); + return flag; + } +}; diff --git a/0112-path-sum/step2.cpp b/0112-path-sum/step2.cpp new file mode 100644 index 0000000..fdb67ea --- /dev/null +++ b/0112-path-sum/step2.cpp @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool hasPathSum(TreeNode* root, int targetSum) { + if (root == nullptr) { + return false; + } + + if (root->left == nullptr && root->right == nullptr) { + return root->val == targetSum; + } + + return hasPathSum(root->left, targetSum - root->val) || + hasPathSum(root->right, targetSum- root->val); + } +}; diff --git a/0112-path-sum/step3.cpp b/0112-path-sum/step3.cpp new file mode 100644 index 0000000..61b1bd0 --- /dev/null +++ b/0112-path-sum/step3.cpp @@ -0,0 +1,25 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool hasPathSum(TreeNode* root, int targetSum) { + if (root == nullptr) { + return false; + } + if (root->left == nullptr && root->right == nullptr) { + return root->val == targetSum; + } + + return hasPathSum(root->left, targetSum - root->val) || + hasPathSum(root->right, targetSum - root->val); + } +};