Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions maximum-depth-of-binary-tree/Jeehay28.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오......자스 문법 활용이 제대로 되서 아주 이쁘네요...!

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Depth-First Search (DFS) with recursion

// 🕒 Time Complexity: O(n) — where n is the number of nodes in the binary tree.
// 🗂️ Space Complexity: O(h) — where h is the height of the tree.
// ⚙️ How It Works (Example Walkthrough):
// For root = [3,9,20,null,null,15,7]:
// maxDepth(root) = Math.max(maxDepth(9), maxDepth(20)) + 1 = Math.max(1, 2) + 1 = 3
// maxDepth(9) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
// maxDepth(20) = Math.max(maxDepth(15), maxDepth(7)) + 1 = Math.max(1, 1) + 1 = 2
// maxDepth(15) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
// maxDepth(7) = Math.max(maxDepth(null), maxDepth(null)) + 1 = 1
// So the final result: 3

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = val === undefined ? 0 : val;
* this.next = next === undefined ? null : next;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/

// For maximum depth, the presence of one child doesn't matter much because we are looking for the deepest path from the root to any leaf.
var maxDepth = function (root) {
// Base case: if the node is null, the depth is 0
if (root === null) return 0;

// Recursively calculate the depth of the left and right subtrees
let leftDepth = maxDepth(root.left);
let rightDepth = maxDepth(root.right);

// Return the maximum of the two depths plus 1 for the current node
return Math.max(leftDepth, rightDepth) + 1;
};

43 changes: 43 additions & 0 deletions reorder-list/Jeehay28.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stack을 활용해서 문제를 풀이 해 주셨네요!
해당 문제는 LinkedList의 특징을 활용하여 풀어주시면 공간 복잡도를 더 줄여서 풀이가 가능할 것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ✅ Time Complexity: O(N)
// ✅ Space Complexity: O(N) (due to the stack storage)

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/

/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function (head) {
let stack = [];
let node = head;

while (node) {
stack.push(node);
node = node.next;
}

let dummy = new ListNode(-1);
node = dummy;

const len = stack.length;

for (let i = 0; i < len; i++) {
if (i % 2 === 1) {
node.next = stack.pop();
} else {
node.next = head;
head = head.next;
}
node = node.next;
}

node.next = null;
return dummy.next;
};