-
-
Notifications
You must be signed in to change notification settings - Fork 245
[YoungSeok-Choi] Week 10 Solutions #1539
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c28c2d
add: linked-list-cycle
Yongseok-Choi-dev 6935221
add: [week-8] clong-graph
Yongseok-Choi-dev 8fd01f4
add: Pacific Atlantic Water Flow
Yongseok-Choi-dev 6c4214a
add: maximon product subarry
Yongseok-Choi-dev 77616b9
fix: lint
Yongseok-Choi-dev 32260fe
add: lcs, max product subArray
Yongseok-Choi-dev b50811c
add: invert bin tree
Yongseok-Choi-dev 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,71 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode() { | ||
} | ||
|
||
TreeNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
TreeNode(int val, TreeNode left, TreeNode right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
|
||
// tc -> O(n) | ||
class Solution { | ||
|
||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) | ||
return null; | ||
|
||
TreeNode left = invertTree(root.left); | ||
TreeNode right = invertTree(root.right); | ||
|
||
root.right = left; | ||
root.left = right; | ||
|
||
return root; | ||
} | ||
|
||
} | ||
|
||
// NOTE: deep copy 작업도 해볼 것 | ||
// 같은 val의 Node가 여러 개 들어오는 경우를 고려하지 못했음.. | ||
class WrongSolution { | ||
|
||
public Map<Integer, TreeNode> tMap = new HashMap<>(); | ||
|
||
public TreeNode invertTree(TreeNode root) { | ||
if (root == null) | ||
return null; | ||
|
||
int rVal = root.val; | ||
|
||
tMap.computeIfAbsent(rVal, k -> new TreeNode(k)); | ||
|
||
TreeNode cur = tMap.get(rVal); | ||
|
||
if (root.left != null) { | ||
tMap.computeIfAbsent(root.left.val, k -> new TreeNode(k)); | ||
cur.right = tMap.get(root.left.val); | ||
invertTree(root.left); | ||
} | ||
|
||
if (root.right != null) { | ||
tMap.computeIfAbsent(root.right.val, k -> new TreeNode(k)); | ||
cur.left = tMap.get(root.right.val); | ||
invertTree(root.right); | ||
} | ||
|
||
return tMap.get(root.val); | ||
} | ||
} |
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.
재귀를 이용한 풀이가 정말 깔끔하네요 🤩 혹시 여유가 되신다면 큐와 반복문을 이용한 방식으로도 추가로 풀어보셔도 좋을 것 같습니다~!