-
Notifications
You must be signed in to change notification settings - Fork 0
112. Path Sum #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
112. Path Sum #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| Solve Time : 03:57 | ||
|
|
||
| Time : O(N) | ||
| Space : O(N) | ||
|
|
||
| 再帰でざっくりと実装、特に悩むことなく実装完了 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int target_sum) { | ||
| if (!root) { | ||
| return false; | ||
| } | ||
| if (!root->left && !root->right) { | ||
| return target_sum == root->val; | ||
| } | ||
| return hasPathSum(root->left, target_sum - root->val) || hasPathSum(root->right, target_sum - root->val); | ||
| } | ||
| }; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| Time : O(N) | ||
| Space : O(N) | ||
|
|
||
| 再帰ではなく、stackを使って実装 | ||
| */ | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int target_sum) { | ||
| stack<pair<TreeNode*, int>> nodes_and_sums; | ||
| nodes_and_sums.emplace(root, target_sum); | ||
| while (!nodes_and_sums.empty()) { | ||
| auto [node, sum] = nodes_and_sums.top(); | ||
| nodes_and_sums.pop(); | ||
| if (!node) { | ||
| continue; | ||
| } | ||
| if (!node->left && !node->right && sum == node->val) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sum == target_sumとなるかと思ったら違ったので少し戸惑いました。sumという変数名を使うなら足し上げていくか、odaさんも指摘されているようにcomplementやrest_sumのような「残り」を意味する変数名を使うという選択肢があると思います
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. コメントありがとうございます。 |
||
| return true; | ||
| } | ||
| nodes_and_sums.emplace(node->left, sum - node->val); | ||
| nodes_and_sums.emplace(node->right, sum - node->val); | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int target_sum) { | ||
| if (!root) { | ||
| return false; | ||
| } | ||
| if (!root->left && !root->right) { | ||
| return target_sum == root->val; | ||
| } | ||
| return hasPathSum(root->left, target_sum - root->val) || hasPathSum(root->right, target_sum - root->val); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sum -= node->val
先にしたほうが素直ではないですかね。どちらでもいいですが。あと、名前的には、sum というよりは残りですかね。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.ed3x3pkyeqkp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
減算しているので残りという概念のほうが適切ですね、ありがとうございます。