<commit_before># Time:  O(|V| + |E|)
# Space: O(|V|)

# BFS solution.
class Solution:
    # @param {integer} n
    # @param {integer[][]} edges
    # @return {boolean}
    def validTree(self, n, edges):
        if len(edges) != n - 1:
            return False

        visited_from, neighbors = 0, 1
        nodes = {}  # A structure to track each node's [visited_from, neighbors]
        for i in xrange(n):
            nodes[i] = [-1, []]
        for edge in edges:
            nodes[edge[0]][neighbors].append(edge[1])
            nodes[edge[1]][neighbors].append(edge[0])

        # BFS to check whether the graph is valid tree.
        visited = {}
        q = collections.deque()
        if edges:
            q.append(edges[0][0])
        while q:
            i = q.popleft()
            visited[i] = True
            for n in nodes[i][neighbors]:
                if n != nodes[i][visited_from]:
                    if n in visited:
                        return False
                    else:
                        visited[n] = True
                        nodes[n][visited_from] = i
                        q.append(n)
        return True
<commit_msg>Update graph-valid-tree.py<commit_after># Time:  O(|V| + |E|)
# Space: O(|V|)

# BFS solution.
class Solution:
    # @param {integer} n
    # @param {integer[][]} edges
    # @return {boolean}
    def validTree(self, n, edges):
        if len(edges) != n - 1:
            return False
        elif n == 1:
            return True

        visited_from, neighbors = 0, 1
        nodes = {}  # A structure to track each node's [visited_from, neighbors]
        for i in xrange(n):
            nodes[i] = [-1, []]
        for edge in edges:
            nodes[edge[0]][neighbors].append(edge[1])
            nodes[edge[1]][neighbors].append(edge[0])

        # BFS to check whether the graph is valid tree.
        visited = {}
        q = collections.deque()
        q.append(edges[0][0])
        while q:
            i = q.popleft()
            visited[i] = True
            for n in nodes[i][neighbors]:
                if n != nodes[i][visited_from]:
                    if n in visited:
                        return False
                    else:
                        visited[n] = True
                        nodes[n][visited_from] = i
                        q.append(n)
        return True

{'fraction_non_alphanumeric': 0.1239424703891709, 'fraction_numerical': 0.008883248730964468, 'mean_word_length': 1.3961499493414387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5598890', 'n_tokens_mistral': 729, 'n_tokens_neox': 703, 'n_words': 194}