-
Notifications
You must be signed in to change notification settings - Fork 1
[Leetcode - Easy] Path sum #33
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @param {number} targetSum | ||
| * @return {boolean} | ||
| */ | ||
| var hasPathSum = function (root, targetSum) { | ||
| let answer = false; | ||
|
|
||
| const dfs = (tree, curr) => { | ||
| if (tree.left) dfs(tree.left, curr + tree.left.val); | ||
| if (tree.right) dfs(tree.right, curr + tree.right.val); | ||
| if (!tree.left && !tree.right && curr === targetSum) { | ||
| answer = true; | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| if (root) dfs(root, root.val); | ||
| return answer; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
재귀는 탈출조건을 가장 상단에 적는다고 알고있는데 아래에 배치하셨네요 이러면 리프노드에서 한단계 더 밑으로 내려가서 검사를 진행한 이후에 진행이 될거같은데 맞나요?
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.
targetSum과 비교dfs함수가 결국 위 두가지로 나뉘기 때문에, 재귀 함수 내부의 배치 순서가 크게 중요하지 않다고 생각했던 것 같습니다.근데 생각해보니까 함수의 최하단에 배치되어 있어 탈출을하는 경우에도 결국 모든 라인을 해석한 후 탈출하게 되겠네요. 짚어주셔서 감사합니다 ㅎㅎ