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