Skip to content

Commit

Permalink
Merge pull request #551 from jaejeong1/main
Browse files Browse the repository at this point in the history
[jaejeong1] WEEK 11 Solutions
  • Loading branch information
jaejeong1 authored Oct 25, 2024
2 parents 64495db + 5a0b4ee commit e6c6b28
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
44 changes: 44 additions & 0 deletions invert-binary-tree/jaejeong1.java
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;
}
}
41 changes: 41 additions & 0 deletions maximum-depth-of-binary-tree/jaejeong1.java
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);
}
}
54 changes: 54 additions & 0 deletions reorder-list/jaejeong1.java
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;
}
}
}

0 comments on commit e6c6b28

Please sign in to comment.