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+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * ์ฃผ์ด์ง ์ด์ง ํธ๋ฆฌ์ ์ต๋ ๊น์ด๋ฅผ ๋ฐํํ๋ ํจ์
11+ * @param {TreeNode } root
12+ * @return {number }
13+ */
14+ const maxDepth = function ( root ) {
15+ let maxDepth = 0 ;
16+
17+ function bfs ( node , depth ) {
18+ if ( ! node ) return ;
19+
20+ depth += 1 ;
21+ if ( maxDepth < depth ) maxDepth = depth ;
22+ bfs ( node . left , depth ) ;
23+ bfs ( node . right , depth ) ;
24+ }
25+
26+ bfs ( root , maxDepth ) ;
27+ return maxDepth ;
28+ } ;
29+
30+ // ์๊ฐ๋ณต์ก๋: O(n)
31+ // ๊ณต๊ฐ๋ณต์ก๋: O(h) (h: ํธ๋ฆฌ์ ๋์ด. ์ต์
์ ๊ฒฝ์ฐ ํธํฅํธ๋ฆฌ์ผ ๋ h===n์ผ๋ก O(n))
You canโt perform that action at this time.
0 commit comments