-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Tony] WEEK 14 Solutions #592
Changes from 2 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,26 @@ | ||
// TC: O(n) | ||
// need to visit all nodes | ||
// SC: O(n) | ||
// normally O(log n) required however, O(n) in the worst case | ||
class Solution { | ||
private List<List<Integer>> output = new ArrayList<>(); | ||
public List<List<Integer>> levelOrder(TreeNode root) { | ||
dsf(0, root); | ||
return output; | ||
} | ||
|
||
private void dsf(int level, TreeNode node) { | ||
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. 문제를 보고 저는 자연스레 BFS가 떠올라서 BFS로 풀이했는데, 이렇게 DFS로 풀이할 수도 있군요 ㅎㅎ 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. 앗 저는 DFS밖에 생각이 안나서 ㅋㅋㅋ |
||
if (node == null) return; | ||
|
||
if (output.size() == level) { | ||
List<Integer> inside = new ArrayList<>(); | ||
inside.add(node.val); | ||
output.add(inside); | ||
} else { | ||
output.get(level).add(node.val); | ||
} | ||
level += 1; | ||
dsf(level, node.left); | ||
dsf(level, node.right); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Solution { | ||
public int reverseBits(int n) { | ||
// time complexity O(1) | ||
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. 엇 time complexity가 O(N) 아닌가요? 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. for문의 반복 횟수가 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. 오 맞네요 제가 생각이 짧았습니다 ㅎㅎㅎ |
||
// space complexity O(1) | ||
int output = 0; | ||
|
||
for (int i = 0; i < Integer.SIZE; i++) { | ||
output <<= 1; | ||
output += n & 1; | ||
n >>= 1; | ||
} | ||
return output; | ||
} | ||
} |
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.
(완전완전 사소한 typo)
dsf
->dfs
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.
이런... 수정해두겠습니다 ㅠ 오타가 제일 무서워요