Skip to content

Commit 2b1cf5a

Browse files
author
jinvicky
committed
add comment
1 parent 6b7469b commit 2b1cf5a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

โ€Žmaximum-depth-of-binary-tree/jinvicky.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•œ ๋‹ค์Œ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ํƒ์ƒ‰์„ ์‹œ์ž‘
2+
// 1 (root)
3+
// / \
4+
// 2 3
5+
// / \ / \
6+
// 4 5 6 7
7+
//
8+
// root๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ๋…ธ๋“œ๋ณ„๋กœ left, right์„ ๋˜ ์žฌ๊ท€๋กœ ํ˜ธ์ถœํ•˜๋‹ˆ
9+
// (1)์˜ left, right, (2)์˜ left, right, (3)์˜ left, right, (4)์˜ left, right์ด ์‹คํ–‰๋œ๋‹ค.
10+
// ๊ฒฐ๊ณผ์ ์œผ๋กœ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ depth์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ depth + ์ตœ์ดˆ root์˜ depth 1์„ ํ•ฉํ•ด์„œ ์™„์„ฑ
11+
// Math.max()๋Š” ํ•จ์ˆ˜ ์•ˆ์˜ ๋ชจ๋“  ๊ฐ’์ด ๊ณ„์‚ฐ๋˜์–ด์•ผ๋งŒ ์‹คํ–‰๋œ๋‹ค.
112
class Solution {
213
// ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ BFS๋ณด๋‹ค DFS๊ฐ€ ๋” ์ ์ ˆํ•ฉ๋‹ˆ๋‹ค.
314
// ๋งค ๋…ธ๋“œ์˜ left, right์„ ์žฌ๊ท€๋กœ ํ˜ธ์ถœํ•ด์„œ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์žฌ๊ท€ ํ•จ์ˆ˜ ํ•„์š”.

โ€Žword-search/jinvicky.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,55 @@ boolean dfs(char[][] board,int i,int j,String word,int index){
2828
return found;
2929
}
3030

31+
// 2์ฐจ์› ๋ฐฉ๋ฌธ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  direction ๋ฐฉํ–ฅ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ํ’€์ด
32+
public boolean exist2(char[][] board, String word) {
33+
int m = board.length;
34+
int n = board[0].length;
35+
boolean[][] visited = new boolean[m][n];
36+
boolean result = false;
37+
38+
for (int i = 0; i < m; i++) {
39+
for (int j = 0; j < n; j++) {
40+
if (board[i][j] == word.charAt(0)) {
41+
result = backtrack(board, word, visited, i, j, 0);
42+
if (result)
43+
return true;
44+
}
45+
}
46+
}
47+
48+
return false;
49+
}
50+
51+
private boolean backtrack(char[][] board, String word, boolean[][] visited, int i, int j, int index) {
52+
if (index == word.length()) {
53+
return true;
54+
}
55+
56+
// dfs๋ฅผ ํ’€ ๋•Œ๋Š” ๋ฐฐ์—ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ break๋ฅผ ๊ผญ ๊ธฐ์–ตํ•˜๊ธฐ
57+
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visited[i][j]
58+
|| board[i][j] != word.charAt(index)) {
59+
return false;
60+
}
61+
62+
visited[i][j] = true;
63+
64+
// if๋ฌธ์—์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ๋ˆˆ์— ์•ˆ ์ต์–ด์„œ
65+
// direction ํ…œํ”Œ๋ฆฟ์„ ๋งŒ๋“ค์–ด์„œ for๋ฌธ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ์•”๊ธฐ๋ฒ•์œผ๋กœ ๋ณ€ํ™˜
66+
int[][] directions = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } }; // ๊ทธ๋ƒฅ ์•”๊ธฐ
67+
68+
for (int[] dir : directions) {
69+
// i๊ฐ€ y, j๊ฐ€ x์ธ๋ฐ ์‚ฌ์‹ค 1, -1, 0๋งŒ ์ž˜ ์„ค์ •ํ•˜๋ฉด ์ƒ๊ด€์—†์Œ. ๋ชฉ์ ์€ 1, -1 1๋ฒˆ์”ฉ ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์œผ๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ
70+
int nextI = i + dir[0];
71+
int nextJ = j + dir[1];
72+
73+
if (backtrack(board, word, visited, nextI, nextJ, index + 1)) {
74+
return true;
75+
}
76+
}
77+
78+
visited[i][j] = false;
79+
return false;
80+
}
81+
3182
}

0 commit comments

Comments
ย (0)