Skip to content

Commit f7801d6

Browse files
author
jinvicky
committed
invertBinaryTree, pacificWaterFlow solution
1 parent bbc3ef9 commit f7801d6

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* public class TreeNode {
7+
* int val;
8+
* TreeNode left;
9+
* TreeNode right;
10+
* TreeNode() {}
11+
* TreeNode(int val) { this.val = val; }
12+
* TreeNode(int val, TreeNode left, TreeNode right) {
13+
* this.val = val;
14+
* this.left = left;
15+
* this.right = right;
16+
* }
17+
* }
18+
*/
19+
class Solution {
20+
public TreeNode invertTree(TreeNode root) {
21+
if (root == null) return null;
22+
23+
TreeNode temp = root.left;
24+
root.left = root.right;
25+
root.right = temp;
26+
27+
System.out.println(root.val);
28+
29+
invertTree(root.left);
30+
invertTree(root.right);
31+
32+
return root;
33+
}
34+
35+
public TreeNode invertTreeByQueue(TreeNode root) {
36+
if (root == null) {
37+
return null;
38+
}
39+
40+
// BFS๋ฅผ ์œ„ํ•œ ํ ์ƒ์„ฑ
41+
Queue<TreeNode> queue = new LinkedList<>();
42+
queue.add(root);
43+
44+
// ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
45+
while (!queue.isEmpty()) {
46+
TreeNode current = queue.poll();
47+
48+
// ํ˜„์žฌ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์ž์‹๊ณผ ์˜ค๋ฅธ์ชฝ ์ž์‹ ๊ตํ™˜
49+
TreeNode temp = current.left;
50+
current.left = current.right;
51+
current.right = temp;
52+
53+
// ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ํ์— ์ถ”๊ฐ€
54+
if (current.left != null) {
55+
queue.add(current.left);
56+
}
57+
if (current.right != null) {
58+
queue.add(current.right);
59+
}
60+
}
61+
62+
return root;
63+
}
64+
}
65+
/**
66+
* public TreeNode invertTreeByQueue(TreeNode root) {
67+
* // root๊ฐ€ ๋„์ด๋ฉด ๋ฆฌํ„ด
68+
* // ํ ์„ ์–ธํ•˜๊ณ  ๋ฃจํŠธ๋ฅผ ์‚ฝ์ž…
69+
*
70+
* // ํ๊ฐ€ ๋น„๊ธฐ ์ „๊นŒ์ง€ ์—ฐ์‚ฐ ๋ฐ˜๋ณต
71+
* // temp ๋ณ€์ˆ˜๋กœ left์™€ right์„ ๊ต์ฒด
72+
* // ์ž์‹ left ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ (์žˆ๋‹ค๋ฉด)
73+
* // ์ž์‹ right ๋…ธ๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€ (์žˆ๋‹ค๋ฉด)
74+
* // ๋ฃจํŠธ๋ฅผ ๋ฐ˜ํ™˜
75+
* }
76+
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class Solution {
5+
private int[][] heights;
6+
private int rows, cols;
7+
private int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // ์ƒํ•˜์ขŒ์šฐ
8+
9+
public List<List<Integer>> pacificAtlantic(int[][] heights) {
10+
this.heights = heights;
11+
this.rows = heights.length;
12+
this.cols = heights[0].length;
13+
14+
// ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘์—์„œ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ง€์ ์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
15+
boolean[][] pacific = new boolean[rows][cols];
16+
boolean[][] atlantic = new boolean[rows][cols];
17+
18+
// ํƒœํ‰์–‘๊ณผ ๋Œ€์„œ์–‘ ๊ฒฝ๊ณ„์—์„œ ํƒ์ƒ‰ ์‹œ์ž‘
19+
// ๋งจ ์œ—์ค„๊ณผ ๋งจ ์•„๋žซ์ค„
20+
for (int j = 0; j < cols; j++) {
21+
dfs(0, j, pacific, Integer.MIN_VALUE); // ํƒœํ‰์–‘ (์œ—์ค„)
22+
dfs(rows - 1, j, atlantic, Integer.MIN_VALUE); // ๋Œ€์„œ์–‘ (์•„๋žซ์ค„)
23+
}
24+
25+
// ๋งจ ์™ผ์ชฝ์ค„๊ณผ ๋งจ ์˜ค๋ฅธ์ชฝ์ค„
26+
for (int i = 0; i < rows; i++) {
27+
dfs(i, 0, pacific, Integer.MIN_VALUE); // ํƒœํ‰์–‘ (์™ผ์ชฝ์ค„)
28+
dfs(i, cols - 1, atlantic, Integer.MIN_VALUE); // ๋Œ€์„œ์–‘ (์˜ค๋ฅธ์ชฝ์ค„)
29+
}
30+
31+
// ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
32+
List<List<Integer>> result = new ArrayList<>();
33+
34+
// ์–‘์ชฝ ๋ฐ”๋‹ค๋กœ ๋ชจ๋‘ ํ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์ง€์  ์ฐพ๊ธฐ
35+
for (int i = 0; i < rows; i++) {
36+
for (int j = 0; j < cols; j++) {
37+
if (pacific[i][j] && atlantic[i][j]) {
38+
List<Integer> coord = new ArrayList<>();
39+
coord.add(i);
40+
coord.add(j);
41+
result.add(coord);
42+
}
43+
}
44+
}
45+
46+
return result;
47+
}
48+
49+
// ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰ (DFS) ํ•จ์ˆ˜
50+
private void dfs(int r, int c, boolean[][] visited, int prevHeight) {
51+
// ์œ ํšจํ•˜์ง€ ์•Š์€ ์ง€์  (๋ฒ”์œ„ ๋ฐ–, ์ด๋ฏธ ๋ฐฉ๋ฌธ, ๋†’์ด ์กฐ๊ฑด ๋ถˆ๋งŒ์กฑ)
52+
if (r < 0 || r >= rows || c < 0 || c >= cols || visited[r][c] || heights[r][c] < prevHeight) {
53+
return;
54+
}
55+
56+
// ํ˜„์žฌ ์ง€์  ๋ฐฉ๋ฌธ ํ‘œ์‹œ
57+
visited[r][c] = true;
58+
59+
// ์ƒํ•˜์ขŒ์šฐ๋กœ ํƒ์ƒ‰ ์ง„ํ–‰
60+
for (int[] dir : directions) {
61+
int newR = r + dir[0];
62+
int newC = c + dir[1];
63+
dfs(newR, newC, visited, heights[r][c]);
64+
}
65+
}
66+
}

0 commit comments

Comments
ย (0)