MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / valid_tree

Method valid_tree

python/0261-graph-valid-tree.py:60–71  ·  view source on GitHub ↗
(self, n: int, edges: List[List[int]])

Source from the content-addressed store, hash-verified

58 self.components -= 1
59
60 def valid_tree(self, n: int, edges: List[List[int]]) -> bool:
61 # init here as not sure that ctor will be re-invoked in different tests
62 self.parents = {}
63 self.heights = {}
64 self.components = n
65
66 for e1, e2 in edges:
67 if self.__find(e1) == self.__find(e2): # 'redundant' edge
68 return False
69 self.__connect(e1, e2)
70
71 return self.components == 1 # forest contains one tree
72
73

Callers

nothing calls this directly

Calls 2

__findMethod · 0.95
__connectMethod · 0.95

Tested by

no test coverage detected