diff --git a/doitchuu/BalancedBinaryTree.js b/doitchuu/BalancedBinaryTree.js new file mode 100644 index 0000000..542be12 --- /dev/null +++ b/doitchuu/BalancedBinaryTree.js @@ -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; +}; diff --git a/doitchuu/ImplementQueueUsingStacks.js b/doitchuu/ImplementQueueUsingStacks.js new file mode 100644 index 0000000..e17cee3 --- /dev/null +++ b/doitchuu/ImplementQueueUsingStacks.js @@ -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) { + 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() + */ diff --git a/doitchuu/LinkedListCycle.js b/doitchuu/LinkedListCycle.js new file mode 100644 index 0000000..d3e6af5 --- /dev/null +++ b/doitchuu/LinkedListCycle.js @@ -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; +}; diff --git a/doitchuu/LowestCommonAncestorOfABinarySearchTree.js b/doitchuu/LowestCommonAncestorOfABinarySearchTree.js new file mode 100644 index 0000000..67785ad --- /dev/null +++ b/doitchuu/LowestCommonAncestorOfABinarySearchTree.js @@ -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; +};