File tree Expand file tree Collapse file tree
maximum-depth-of-binary-tree Expand file tree Collapse file tree 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+ class Solution {
17+ public int maxDepth (TreeNode root ) {
18+ /**
19+ 문제: 바이너리트리의 maximum depth 구하기
20+ 조건
21+ - 노드의 개수는 0 ~ 10^4
22+ - node val 은 -100 ~ 100
23+ 풀이: dfs 로 depth 구하기
24+ - 주어진 트리는 binary tree
25+ - time complexity : O(n) , O(log n)
26+ - space complexity : O(h)
27+ */
28+
29+
30+ if (root == null ) return 0 ;
31+
32+ int height = dfs (root );
33+ return height ;
34+ }
35+
36+ int dfs (TreeNode node ) {
37+ if (node == null ) return 0 ;
38+
39+ int left = dfs (node .left );
40+ int right = dfs (node .right );
41+
42+ return Math .max (left , right ) + 1 ;
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments