Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d484c9a

Browse files
added Check Valid Tree Given Edges (Others)
1 parent 435e5c3 commit d484c9a

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Check Valid Tree Given Edges
2+
3+
## Problem:
4+
5+
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
6+
7+
## Example:
8+
9+
```
10+
Input: n = 4, edges = [(0,1), (2,0), (3,2)]
11+
Output: true
12+
0
13+
/ \
14+
1 2
15+
/
16+
3
17+
18+
Input: n = 4, edges = [(0,1), (0,2), (2,1), (0,3)]
19+
Output: false
20+
0
21+
/ | \
22+
1 - 2 3
23+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def isValidEdges(n, edges):
2+
connected = set()
3+
for a, b in edges:
4+
if a in connected and b in connected:
5+
return False
6+
connected.add(a)
7+
connected.add(b)
8+
return True
9+
10+
11+
n = 4
12+
edges = [(0, 1), (2, 0), (3, 2)]
13+
print(isValidEdges(n, edges))
14+
15+
edges = [(0, 1), (0, 2), (2, 1), (0, 3)]
16+
print(isValidEdges(n, edges))

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Languages used: Java and Python
9292
- [Reverse Linked List II](Medium/ReverseLinkedList2)
9393
- [Binary Tree Postorder Traversal](Medium/BinaryPostorder)
9494
- Hard
95+
9596
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
9697
- [Reducing Dishes](Hard/ReducingDishes)
9798
- [Longest Consecutive Sequence](Hard/LongestConsecutiveSequence)
@@ -101,6 +102,9 @@ Languages used: Java and Python
101102
- [Escape a Large Maze](Hard/EscapeLargeMaze)
102103
- [Serialize and Deserialize Binary Tree](Hard/SerializeAndDeserializeBinaryTree)
103104

105+
- Others (Non-leetcode)
106+
- [Check Valid Tree Given Edges](Others/CheckValidTreeGivenEdges)
107+
104108
---
105109

106110
# Challenge_README_Template

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /