File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
binary-tree-level-order-traversal Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ // ์ฌ๊ท ํจ์๋ก ํด๊ฒฐ
2
+ // ๊ฐ ๋
ธ๋์ ๋ ๋ฒจ์ ๊ธฐ์ตํ๊ณ ์๋ค๊ฐ ์ ๋ต๋ณ์์ ๋ ๋ฒจ๋ณ๋ก ๋
ธ๋๊ฐ์ ๋ฃ์ด์ค
3
+ class Solution {
4
+ func levelOrder( _ root: TreeNode ? ) -> [ [ Int ] ] {
5
+ var answer = [ [ Int] ] ( )
6
+ searchNodes ( 0 , root, & answer)
7
+ return answer
8
+ }
9
+
10
+ func searchNodes( _ level: Int , _ node: TreeNode ? , _ answer: inout [ [ Int ] ] ) {
11
+ guard let node else { return }
12
+ if level >= answer. count { answer. append ( [ ] ) }
13
+ answer [ level] . append ( node. val)
14
+ searchNodes ( level + 1 , node. left, & answer)
15
+ searchNodes ( level + 1 , node. right, & answer)
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ // time: O(n)
2
+ // approach - 2์๋ฆฌ 3์๋ฆฌ ... ๋ก 2์ง๋ฒ ๋ณํ์ ๋งจ ์ 1์ ๊ณ ์ ์ด๋ค.
3
+ // ๊ทธ ์ดํ ๋๋จธ์ง๋ 0~N์๋ฆฌ์ผ๋ ์ด 1์ ๊ฐฏ์ ๋ฅผ ๋ํด์คฌ๋ค.
4
+ class Solution {
5
+ func countBits( _ n: Int ) -> [ Int ] {
6
+ var answer : [ Int ] = [ 0 , 1 ]
7
+
8
+ while answer. count < n + 1 {
9
+ answer += answer. map { $0 + 1 }
10
+ }
11
+
12
+ return [ Int] ( answer. prefix ( n + 1 ) )
13
+ }
14
+ }
You canโt perform that action at this time.
0 commit comments