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.
1#include <stdbool.h>
2
3int find(int* parent, int x) {
4 if (parent[x] != x) {
5 parent[x] = find(parent, parent[x]); // Path compression
6 }
7 return parent[x];
8}
9
10bool unionFind(int* parent, int x, int y) {
11 int rootX = find(parent, x);
12 int rootY = find(parent, y);
13
14 if (rootX == rootY) return false; // Cycle detected
15 parent[rootY] = rootX; // Union
16 return true;
17}
18
19bool validateBinaryTreeNodes(int n, int* leftChild, int leftChildSize, int* rightChild, int rightChildSize) {
20 int parent[n];
21 for (int i = 0; i < n; ++i) {
22 parent[i] = i;
23 }
24
25 int hasParent[n];
26 for (int i = 0; i < n; ++i) {
27 hasParent[i] = 0;
28 }
29
30 for (int i = 0; i < n; ++i) {
31 if (leftChild[i] != -1) {
32 if (hasParent[leftChild[i]] || !unionFind(parent, i, leftChild[i])) {
33 return false;
34 }
35 hasParent[leftChild[i]] = 1;
36 }
37 if (rightChild[i] != -1) {
38 if (hasParent[rightChild[i]] || !unionFind(parent, i, rightChild[i])) {
39 return false;
40 }
41 hasParent[rightChild[i]] = 1;
42 }
43 }
44
45 int rootCount = 0;
46 for (int i = 0; i < n; ++i) {
47 if (!hasParent[i]) {
48 ++rootCount;
49 }
50 }
51
52 return rootCount == 1;
53}
The solution creates a parent
array to track the roots of each node using the find function with path compression. The unionFind
function unites two components. If unionFind
finds that two nodes are already connected, it indicates a cycle. The solution verifies each node only has one parent and no cycle exists by unionizing their parent-child relations. Finally, it ensures exactly one node has no parent (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.
1public class Solution {
2 private void Dfs(int node, bool[] visited, int[][] children) {
3 if (visited[node]) return;
4 visited[node] = true;
5 if (children[0][node] != -1) Dfs(children[0][node], visited, children);
6 if (children[1][node] != -1) Dfs(children[1][node], visited, children);
7 }
8
9 public bool ValidateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
10 int[] inDegree = new int[n];
11 for (int i = 0; i < n; i++) {
12 if (leftChild[i] != -1) inDegree[leftChild[i]]++;
13 if (rightChild[i] != -1) inDegree[rightChild[i]]++;
14 }
15
16 int root = -1;
17 for (int i = 0; i < n; i++) {
18 if (inDegree[i] == 0) {
19 if (root == -1) {
20 root = i;
21 } else {
22 return false;
23 }
24 }
25 }
26
27 if (root == -1) return false;
28
29 bool[] visited = new bool[n];
30 Dfs(root, visited, new int[][] { leftChild, rightChild });
31
32 foreach (bool v in visited) {
33 if (!v) return false;
34 }
35
36 return true;
37 }
38}
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.