Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions sik9252/BalancedBinaryTree.js
Copy link
Collaborator

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,19 @@
var isBalanced = function (root) {
function getHeight(node) {
if (node === null) return 0;
return Math.max(getHeight(node.left), getHeight(node.right)) + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

와 이런 코드는 어떻게 생각하시는 거죠?! 한 수 배워가요

}
Comment on lines +2 to +5
Copy link
Member

Choose a reason for hiding this comment

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

높이 구하는 부분을 함수로 분리하셨네요 👍 깔끔!


function check(node) {
if (node === null) return true;

const leftH = getHeight(node.left);
const rightH = getHeight(node.right);

if (Math.abs(leftH - rightH) > 1) return false;

return check(node.left) && check(node.right);
}

return check(root);
};
57 changes: 57 additions & 0 deletions sik9252/ImplementQueueUsingStacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var MyQueue = function () {
this.stack = [];
};

/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function (x) {
this.stack.push(x);
};

/**
* @return {number}
*/
MyQueue.prototype.pop = function () {
if (this.stack.length === 1) {
return this.stack.pop();
}

const top = this.stack.pop();
const result = this.pop();
this.stack.push(top);

return result;
};

/**
* @return {number}
*/
MyQueue.prototype.peek = function () {
if (this.stack.length === 1) {
return this.stack[this.stack.length - 1];
}

const top = this.stack.pop();
const result = this.peek();
Copy link
Member

Choose a reason for hiding this comment

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

재귀를 잘 쓰시는 군요..... 👍
스택 하나로 queue 구현은 이렇게 할 수 있겠네요!
다른 풀이법 하나 참고할 수 있어 좋네요! 💯

this.stack.push(top);

return result;
};

/**
* @return {boolean}
*/
MyQueue.prototype.empty = function () {
return this.stack.length === 0;
};

/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/
15 changes: 15 additions & 0 deletions sik9252/LinkedListCycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var hasCycle = function (head) {
const visited = new Set();
let cur = head;

while (cur !== null) {
if (visited.has(cur)) {
return true;
}

visited.add(cur);
cur = cur.next;
}

return false;
};
17 changes: 17 additions & 0 deletions sik9252/LowestCommonAncestorOfBinarySearchTree.js
Copy link
Collaborator

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,17 @@
var lowestCommonAncestor = function (root, p, q) {
let cur = root;
const low = Math.min(p.val, q.val);
const high = Math.max(p.val, q.val);

while (cur !== null) {
if (high < cur.val) {
cur = cur.left;
} else if (low > cur.val) {
cur = cur.right;
} else {
return cur;
}
}

return null;
};