-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #551 from jaejeong1/main
[jaejeong1] WEEK 11 Solutions
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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(N) | ||
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; | ||
} | ||
} | ||
} |