-
Notifications
You must be signed in to change notification settings - Fork 1
[WEEK03] 이지현 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WEEK03] 이지현 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와 이런 코드는 어떻게 생각하시는 거죠?! 한 수 배워가요 |
||
| } | ||
|
Comment on lines
+2
to
+5
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| }; | ||
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 재귀를 잘 쓰시는 군요..... 👍 |
||
| 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() | ||
| */ | ||
| 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; | ||
| }; |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오래 걸리시긴해도 풀이가 가능하신 게 넘 부럽습니다.