-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jaejeong1] WEEK 11 Solutions #551
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,44 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public 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; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
// 풀이: child node로 내려가면서 재귀로 계속 left와 right를 바꿔준다. | ||
// TC: O(N) | ||
// SC: O(N) | ||
public TreeNode invertTree(TreeNode root) { | ||
return invert(root); | ||
} | ||
|
||
private TreeNode invert(TreeNode node) { | ||
if (node == null) { | ||
return node; | ||
} | ||
|
||
node = swap(node); | ||
|
||
invert(node.left); | ||
invert(node.right); | ||
|
||
return node; | ||
} | ||
|
||
private TreeNode swap(TreeNode node) { | ||
var temp = node.left; | ||
node.left = node.right; | ||
node.right = temp; | ||
|
||
return node; | ||
} | ||
} |
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,41 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public 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; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
// 풀이 : DFS 탐색을 통해 리프 노드까지 탐색하면서 깊이를 계산한다. | ||
// TC: O(N), SC: O(N) | ||
int answer = 0; | ||
|
||
public int maxDepth(TreeNode root) { | ||
dfs(root, 1); | ||
|
||
return answer; | ||
} | ||
|
||
private void dfs(TreeNode node, int depth) { | ||
if (node == null) { | ||
return; | ||
} | ||
|
||
if (depth > answer) { | ||
answer = depth; | ||
} | ||
|
||
depth++; | ||
|
||
dfs(node.left, depth); | ||
dfs(node.right, depth); | ||
} | ||
} |
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,54 @@ | ||
import java.util.Stack; | ||
|
||
// Definition for singly-linked list. | ||
class ListNode { | ||
|
||
int val; | ||
ListNode next; | ||
|
||
ListNode() { | ||
} | ||
|
||
ListNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
ListNode(int val, ListNode next) { | ||
this.val = val; | ||
this.next = next; | ||
} | ||
} | ||
|
||
class Solution { | ||
|
||
public void reorderList(ListNode head) { | ||
// 풀이: 역순으로 저장할 스택에 node들을 넣고, 기존 node 1개/스택 node 1개씩 이어 붙인다 | ||
// 스택은 LIFO로 저장되기 때문에, 문제에서 요구하는 순서대로 reorderList를 만들 수 있다 | ||
// TC: O(N) | ||
// SC: O(2N) | ||
Stack<ListNode> stack = new Stack<>(); | ||
|
||
var curNode = head; | ||
while(curNode != null) { | ||
stack.push(curNode); | ||
curNode = curNode.next; | ||
} | ||
|
||
curNode = head; | ||
var halfSize = stack.size() / 2; // 한번에 2개씩 연결하기때문에 절반까지만 돌면 됨 | ||
for (int i=0; i<halfSize; i++) { | ||
var top = stack.pop(); | ||
|
||
var nextNode = curNode.next; | ||
curNode.next = top; | ||
top.next = nextNode; | ||
|
||
curNode = nextNode; | ||
} | ||
|
||
// 만약 노드의 개수가 홀수면, 하나의 노드가 남기 때문에 next 노드를 null로 처리해줘야 한다. | ||
if (curNode != null) { | ||
curNode.next = null; | ||
} | ||
} | ||
} |
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.
혹시 SC에 O(2N)에서 2가 붙은 이유가 있을까요?
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.
@haklee 기존 ListNode와 새로 생성하는 스택을 모두 더해 2N이라고 표시했는데 기존 ListNode는 원래있던 것이니 N이 맞겠군요..!! 수정했습니다 :)