Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions 0112-path-sum/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### step1

最初にflagをfalseでもっておいて、DFSしながら条件に合うものがあればflagをtrueにするというやり方で実装。

### step2

ChatGPTにflagをもたないやり方に改善してもらった。

### step3

3回通すまで書き直し。
39 changes: 39 additions & 0 deletions 0112-path-sum/step1.cpp
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
5ky7/arai60#22 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘ありがとうございます!こちらに関しては全く意識していなかったので勉強になりました。


void scan(TreeNode* root, int sum, int targetSum) {
if (root == nullptr) {
return;
}
if (root->left == nullptr && root->right == nullptr) {
flag = flag || (sum == targetSum);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag |= (sum == targetSum);

のほうがシンプルだと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。そうですね、そちらの方がシンプルそうです。

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;
}
};
26 changes: 26 additions & 0 deletions 0112-path-sum/step2.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
};
25 changes: 25 additions & 0 deletions 0112-path-sum/step3.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
};