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
43 changes: 43 additions & 0 deletions binary-tree-level-order-traversal/njngwn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// Time Complexity: O(n), n: the number of nodes
// Space Complexity: O(w), w: max width of tree

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> results = new ArrayList<>();
if (root == null) return results;

Queue<TreeNode> levelQueue = new LinkedList<>(); // use queue considering FIFO
levelQueue.offer(root);
Copy link
Contributor

Choose a reason for hiding this comment

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

Queue라는 컬렉션(? 자바에서는 컬렉션이라는 명칭을 사용했던거 같아 사용해봅니다)에서 데이터를 넣을 때, enqueue가 아닌 offer 메서드를 사용하는군요. 아니면 new LinkedList() 를 사용해서 그런건가요?

Copy link
Contributor

Choose a reason for hiding this comment

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

자바에는 Queue가 LinkedList로 구현되어 있는걸로 알고있구요. offer 메소드 사용합니다!


while (!levelQueue.isEmpty()) {
ArrayList<Integer> level = new ArrayList<>(); // node values list for each level
int size = levelQueue.size();

for (int i = 0; i < size; ++i) {
TreeNode node = levelQueue.poll();
level.add(node.val);
if(node.left != null) levelQueue.offer(node.left);
if(node.right != null) levelQueue.offer(node.right);
}

results.add(level);
}

return results;
}
}
11 changes: 11 additions & 0 deletions counting-bits/njngwn.java
Copy link
Contributor

Choose a reason for hiding this comment

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

bit operations 를 사용해서 풀이해 보시면 어떨까요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int[] countBits(int n) {
int[] bitCountArr = new int[n+1];

for (int i = 1; i <= n; ++i) {
bitCountArr[i] = bitCountArr[i/2] + (i & 1);
}

return bitCountArr;
}
}