File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ - ๋ฌธ์
2
+ - ์ ๋ฃ: https://leetcode.com/problems/graph-valid-tree/
3
+ - ๋ฌด๋ฃ: https://www.lintcode.com/problem/178/
4
+ - ํ์ด: https://algorithm.jonghoonpark.com/2024/07/02/leetcode-261
5
+
6
+ ## ๋ด๊ฐ ์์ฑํ ํ์ด
7
+
8
+ ``` java
9
+ public class Solution {
10
+ public boolean validTree (int n , int [][] edges ) {
11
+ int [] degrees = new int [n];
12
+
13
+ for (int [] edge : edges) {
14
+ int largeOne = Math . max(edge[0 ], edge[1 ]);
15
+ if (degrees[largeOne] == 0 ) {
16
+ degrees[largeOne] += 1 ;
17
+ } else {
18
+ return false ;
19
+ }
20
+ }
21
+
22
+ int zeroCount = 0 ;
23
+ for (int degree : degrees) {
24
+ if (degree == 0 ) {
25
+ zeroCount++ ;
26
+ }
27
+
28
+ if (zeroCount > 1 ) {
29
+ return false ;
30
+ }
31
+ }
32
+
33
+ return zeroCount == 1 ;
34
+ }
35
+ }
36
+ ```
37
+
38
+ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ์ ์ ํ๊ณ ํ์๋ค.
39
+
40
+ - ๋ฃจํธ๋ฅผ ์ ์ธํ ๋ชจ๋ ๋
ธ๋๋ 1๊ฐ์ ๋ถ๋ชจ๋ฅผ ๊ฐ์ง๋ค.
41
+ - ๋ฃจํธ๋ ๋ถ๋ชจ๋ฅผ ๊ฐ์ง์ง ์๋๋ค.
42
+
43
+ ๋ฐ๋ผ์ ๊ฐ ๋
ธ๋๋ก ๋ค์ด์ค๋ ์๋ฅผ ์ธ์ด๋ดค์ ๋ ๋ฃจํธ์๋ ๋ค์ด์ค๋ ๊ฐ์ ์ด ์์ด์ผ ํ๋ฉฐ, ๋๋จธ์ง ๋
ธ๋๋ 1๊ฐ์ ๊ฐ์ ์ด ์์ด์ผ ํ๋ค.
44
+
45
+ ### TC, SC
46
+
47
+ ์๊ฐ ๋ณต์ก๋๋ O(n)์ด๋ค. ๊ณต๊ฐ ๋ณต์ก๋๋ O(n)์ด๋ค. ์๊ฐ ๋ณต์ก๋๋ฅผ ์กฐ๊ธ ๋ ๋ํ
์ผ ํ๊ฒ ๋ณด์๋ฉด ๊ฐ์ ์ ์๋ ํฌํจ์ ์ํฌ ์ ์์ ๊ฒ์ด๋ค.
You canโt perform that action at this time.
0 commit comments