Skip to content

Commit 92ac7c7

Browse files
Merge pull request #2112 from CodingTestStudy2/염혜정
[염혜정] 2026/10/01
2 parents 9e831a7 + 68d2525 commit 92ac7c7

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// dfs + 백트래킹
2+
3+
class Solution {
4+
int[] dx = {-1, 1, 0, 0};
5+
int[] dy = {0, 0, -1, 1};
6+
7+
public int getMaximumGold(int[][] grid) {
8+
int max = 0;
9+
for (int i = 0; i<grid.length; i++) {
10+
for (int k = 0; k<grid[0].length; k++) {
11+
if (grid[i][k] == 0) continue;
12+
max = Math.max(max, dfs(grid, i, k));
13+
}
14+
}
15+
return max;
16+
}
17+
18+
int dfs(int[][] grid, int x, int y) {
19+
if (x<0 || y<0 || x>=grid.length || y>=grid[0].length
20+
|| grid[x][y] == 0) return 0;
21+
22+
int temp = grid[x][y];
23+
grid[x][y] = 0;
24+
25+
int max = 0;
26+
for (int i = 0; i<4; i++) {
27+
max = Math.max(max, dfs(grid, x+dx[i], y+dy[i]));
28+
}
29+
grid[x][y] = temp;
30+
31+
return max + temp;
32+
}
33+
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 재귀함수
2+
// O(n)
3+
4+
class Solution {
5+
int max;
6+
public int diameterOfBinaryTree(TreeNode root) {
7+
max = 0;
8+
depth(root);
9+
return max;
10+
}
11+
12+
int depth(TreeNode node) {
13+
if (node == null) return 0;
14+
15+
int left = depth(node.left);
16+
int right = depth(node.right);
17+
max = Math.max(max, left + right);
18+
19+
return Math.max(left, right) + 1;
20+
}
21+
}

0 commit comments

Comments
 (0)