File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description
3
+ * ์ต๋ ๊น์ด๋ฅผ ํ์ํ๋ ๋ฌธ์ ์ฌ์ dfs๋ฅผ ๋จผ์ ๋ ์ฌ๋ ธ์ง๋ง ์ด๋ฒ์ bfs๋ก ํ์ดํ๊ณ ์ถ์ด bfs๋ก ํ์์ต๋๋ค.
4
+ *
5
+ * n = total node count
6
+ * time complexity: O(n)
7
+ * space complexity: O(n)
8
+ */
9
+ var maxDepth = function ( root ) {
10
+ const queue = [ ] ;
11
+ let lastIndex = queue . length ;
12
+ let answer = 0 ;
13
+
14
+ if ( ! root ) return answer ;
15
+
16
+ queue . push ( root ) ;
17
+
18
+ while ( queue . length !== lastIndex ) {
19
+ let currentCount = queue . length - lastIndex ;
20
+ answer ++ ;
21
+
22
+ while ( currentCount -- ) {
23
+ let currentNode = queue [ lastIndex ] ;
24
+ lastIndex ++ ;
25
+ if ( currentNode . left ) queue . push ( currentNode . left ) ;
26
+ if ( currentNode . right ) queue . push ( currentNode . right ) ;
27
+ }
28
+ }
29
+
30
+ return answer ;
31
+ } ;
You canโt perform that action at this time.
0 commit comments