
Sponsored
Sponsored
To solve this problem using the Disjoint Set Union (DSU) approach, we aim to union nodes based on their parent-child relationships. A valid tree should follow these rules:
Time Complexity: O(n), where n is the number of nodes, due to each union and find operation being nearly constant with path compression.
Space Complexity: O(n) for the parent array.
1class Solution:
2 def find(self, parent, x):
3 if parent[x] != x:
4 parent[x] = self.find(parent, parent[x])
5 return parent[x]
6
7 def union(self, parent, x, y):
8 rootX = self.find(parent, x)
9 rootY = self.find(parent, y)
10 if rootX == rootY:
11 return False
12 parent[rootY] = rootX
13 return True
14
15 def validateBinaryTreeNodes(self, n, leftChild, rightChild):
16 parent = list(range(n))
17 hasParent = [0] * n
18
19 for i in range(n):
20 if leftChild[i] != -1:
21 if hasParent[leftChild[i]] or not self.union(parent, i, leftChild[i]):
22 return False
23 hasParent[leftChild[i]] = 1
24 if rightChild[i] != -1:
25 if hasParent[rightChild[i]] or not self.union(parent, i, rightChild[i]):
26 return False
27 hasParent[rightChild[i]] = 1
28
29 return hasParent.count(0) == 1The Python solution follows the union-find pattern, organized into find and union methods. The parent list captures each node’s representative root, while hasParent ensures a node has at most one parent. Disjoint set operations verify no cycles and ensure all nodes unify under one connected root without duplication errors, confirming only one root node.
This approach involves calculating the in-degree of each node and checking connectivity via a DFS. The key aspects of a tree like single-root presence and cycle-checking can be managed by:
Time Complexity: O(n), since each node and its immediate edges are evaluated once in each step, including in-drives calculations and DFS.
Space Complexity: O(n) for holding visited tracking and in-degree counts.
private void Dfs(int node, bool[] visited, int[][] children) {
if (visited[node]) return;
visited[node] = true;
if (children[0][node] != -1) Dfs(children[0][node], visited, children);
if (children[1][node] != -1) Dfs(children[1][node], visited, children);
}
public bool ValidateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
int[] inDegree = new int[n];
for (int i = 0; i < n; i++) {
if (leftChild[i] != -1) inDegree[leftChild[i]]++;
if (rightChild[i] != -1) inDegree[rightChild[i]]++;
}
int root = -1;
for (int i = 0; i < n; i++) {
if (inDegree[i] == 0) {
if (root == -1) {
root = i;
} else {
return false;
}
}
}
if (root == -1) return false;
bool[] visited = new bool[n];
Dfs(root, visited, new int[][] { leftChild, rightChild });
foreach (bool v in visited) {
if (!v) return false;
}
return true;
}
}The C# approach determines the root node based on the inDegree, working under the trademark binary tree outline that each node may only have a single incoming edge (or reference) unless it is the root. Exploiting this relation, all nodes should receive connectivity verification through Dfs, making sure the graph satisfies full extent reachability in step-results.