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
29 changes: 29 additions & 0 deletions doitchuu/BalancedBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isBalanced = function(root) {
function dfs(node) {
if (node === null) return 0;

const left = dfs(node.left);
if (left === -1) return -1;

const right = dfs(node.right);
if (right === -1) return -1;

if (Math.abs(left - right) > 1) return -1;

return Math.max(left, right) + 1;
}

return dfs(root) !== -1;
};
58 changes: 58 additions & 0 deletions doitchuu/ImplementQueueUsingStacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var MyQueue = function () {
this.inStack = [];
this.outStack = [];
};

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

/**
* @return {number}
*/
MyQueue.prototype.pop = function () {
if (this.outStack.length === 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

pop과 peek에서 중복으로 사용되는 로직 분리해도 좋을것 같군용! 다른건 완벽!!

while (this.inStack.length) {
this.outStack.push(this.inStack.pop());
}
}

return this.outStack.pop();
};

/**
* @return {number}
*/
MyQueue.prototype.peek = function () {
if (this.outStack.length === 0) {
while (this.inStack.length) {
this.outStack.push(this.inStack.pop());
}
}

const lastIndex = this.outStack.length - 1;
return this.outStack[lastIndex];
};

/**
* @return {boolean}
*/
MyQueue.prototype.empty = function () {
const hasInStackValues = this.inStack.length !== 0;
const hasOutStackValues = this.outStack.length !== 0;

return !hasInStackValues && !hasOutStackValues ? true : false;
};

/**
* 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()
*/
27 changes: 27 additions & 0 deletions doitchuu/LinkedListCycle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
const isVisited = new Set();
let current = head;

while (current) {
if (isVisited.has(current)) {
return true;
}

isVisited.add(current);
current = current.next;
}

return false;
};
30 changes: 30 additions & 0 deletions doitchuu/LowestCommonAncestorOfABinarySearchTree.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,30 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
const small = Math.min(p.val, q.val);
const big = Math.max(p.val, q.val);

while (root) {
if (big < root.val) {
root = root.left;
} else if (small > root.val) {
root = root.right;
} else {
return root;
}
}

return null;
};