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