File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * class TreeNode {
4+ * val: number
5+ * left: TreeNode | null
6+ * right: TreeNode | null
7+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+ * this.val = (val===undefined ? 0 : val)
9+ * this.left = (left===undefined ? null : left)
10+ * this.right = (right===undefined ? null : right)
11+ * }
12+ * }
13+ */
14+
15+ /**
16+ * ์ด์ง ํธ๋ฆฌ์ ์ต๋ ๊น์ด๋ฅผ ๊ณ์ฐํ๋ ํจ์
17+ * @param {TreeNode | null } root - ํธ๋ฆฌ์ ๋ฃจํธ ๋
ธ๋
18+ * @returns {number } - ํธ๋ฆฌ์ ์ต๋ ๊น์ด (์ต์์ ๋ฃจํธ์์ ๊ฐ์ฅ ๊น์ ๋ฆฌํ๊น์ง ๊น์ด)
19+ *
20+ * ์๊ฐ ๋ณต์ก๋: O(n)
21+ * - ๋ชจ๋ ๋
ธ๋๋ฅผ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธํ์ฌ ๊น์ด๋ฅผ ๊ณ์ฐ
22+ *
23+ * ๊ณต๊ฐ ๋ณต์ก๋: O(h) (h - ํธ๋ฆฌ์ ๋์ด)
24+ */
25+ function maxDepth ( root : TreeNode | null ) : number {
26+ if ( ! root ) return 0 ; // ๋
ธ๋๊ฐ ์์ผ๋ฉด ๊น์ด๋ 0
27+
28+ return Math . max ( maxDepth ( root . left ) , maxDepth ( root . right ) ) + 1 ; // ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ ์ค ๋ ๊น์ ๊ฐ์ +1
29+ } ;
30+
You canโt perform that action at this time.
0 commit comments