File tree Expand file tree Collapse file tree
binary-tree-level-order-traversal Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // BFS 구현
2+ // null 처리 잘 확인할 것...
3+ class Solution {
4+ public List <List <Integer >> levelOrder (TreeNode root ) {
5+ List <List <Integer >> result = new ArrayList <>();
6+ if (root == null ) return result ;
7+
8+ Queue <TreeNode > queue = new LinkedList <>();
9+ queue .offer (root );
10+
11+ while (!queue .isEmpty ()) {
12+ int size = queue .size ();
13+ List <Integer > level = new ArrayList <>();
14+
15+ for (int i = 0 ; i < size ; i ++) {
16+ TreeNode cur = queue .poll ();
17+ level .add (cur .val );
18+
19+ if (cur .left != null ) queue .offer (cur .left );
20+ if (cur .right != null ) queue .offer (cur .right );
21+ }
22+ result .add (level );
23+ }
24+ return result ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+
2+
3+ class Solution {
4+ public int [] countBits (int n ) {
5+ ArrayList <Integer > list = new ArrayList <>();
6+
7+ for (int i = 0 ; i <= n ; i ++) {
8+ int result = i ;
9+ int cnt = 0 ;
10+
11+ while (result > 0 ) {
12+ cnt += result %2 ;
13+ result /= 2 ;
14+ }
15+
16+ list .add (cnt );
17+ }
18+ int [] ans = new int [list .size ()];
19+ for (int i = 0 ; i < list .size (); i ++) {
20+ ans [i ] = list .get (i );
21+ }
22+ return ans ;
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments