Before attempting this problem, you should be comfortable with:
Binary Tree Structure - Understanding parent-child relationships and what makes a valid binary tree
Depth First Search (DFS) - Recursive and iterative tree traversal to visit all nodes
Breadth First Search (BFS) - Level-order traversal using a queue
Indegree Counting - Tracking incoming edges to identify root nodes and detect multiple parents
Union-Find (Disjoint Set Union) - Detecting cycles and verifying connectivity in graph structures
1. Depth First Search
Intuition
A valid binary tree must have exactly one root (a node with no parent), every other node must have exactly one parent, and all nodes must be reachable from the root without cycles. The key insight is that we can identify the root as the only node that never appears as a child, then use DFS to verify that we can reach all n nodes without revisiting any node.
Algorithm
Build a set of all nodes that have a parent by collecting values from leftChild and rightChild arrays.
Find the root by identifying the node that is not in the hasParent set. If all nodes have parents, there is no root, so return false.
Perform dfs starting from the root, tracking visited nodes to detect cycles.
For each node, recursively visit its left and right children if they exist.
If we encounter a node that was already visited, a cycle exists, so return false.
After dfs completes, verify that we visited exactly n nodes to ensure all nodes are connected.
classSolution:defvalidateBinaryTreeNodes(self, n:int, leftChild: List[int], rightChild: List[int])->bool:
hasParent =set(leftChild + rightChild)
hasParent.discard(-1)iflen(hasParent)== n:returnFalse
root =-1for i inrange(n):if i notin hasParent:
root = i
break
visit =set()defdfs(i):if i ==-1:returnTrueif i in visit:returnFalse
visit.add(i)return dfs(leftChild[i])and dfs(rightChild[i])return dfs(root)andlen(visit)== n
publicclassSolution{privateHashSet<int> visit;publicboolValidateBinaryTreeNodes(int n,int[] leftChild,int[] rightChild){HashSet<int> hasParent =newHashSet<int>();foreach(int c in leftChild)if(c !=-1) hasParent.Add(c);foreach(int c in rightChild)if(c !=-1) hasParent.Add(c);if(hasParent.Count == n)returnfalse;int root =-1;for(int i =0; i < n; i++){if(!hasParent.Contains(i)){
root = i;break;}}
visit =newHashSet<int>();returnDfs(root, leftChild, rightChild)&& visit.Count == n;}privateboolDfs(int i,int[] leftChild,int[] rightChild){if(i ==-1)returntrue;if(visit.Contains(i))returnfalse;
visit.Add(i);returnDfs(leftChild[i], leftChild, rightChild)&&Dfs(rightChild[i], leftChild, rightChild);}}
funcvalidateBinaryTreeNodes(n int, leftChild []int, rightChild []int)bool{
hasParent :=make(map[int]bool)for_, c :=range leftChild {if c !=-1{
hasParent[c]=true}}for_, c :=range rightChild {if c !=-1{
hasParent[c]=true}}iflen(hasParent)== n {returnfalse}
root :=-1for i :=0; i < n; i++{if!hasParent[i]{
root = i
break}}
visit :=make(map[int]bool)var dfs func(i int)bool
dfs =func(i int)bool{if i ==-1{returntrue}if visit[i]{returnfalse}
visit[i]=truereturndfs(leftChild[i])&&dfs(rightChild[i])}returndfs(root)&&len(visit)== n
}
class Solution {privateval visit = HashSet<Int>()funvalidateBinaryTreeNodes(n: Int, leftChild: IntArray, rightChild: IntArray): Boolean {val hasParent = HashSet<Int>()for(c in leftChild)if(c !=-1) hasParent.add(c)for(c in rightChild)if(c !=-1) hasParent.add(c)if(hasParent.size == n)returnfalsevar root =-1for(i in0 until n){if(i !in hasParent){
root = i
break}}
visit.clear()returndfs(root, leftChild, rightChild)&& visit.size == n
}privatefundfs(i: Int, leftChild: IntArray, rightChild: IntArray): Boolean {if(i ==-1)returntrueif(i in visit)returnfalse
visit.add(i)returndfs(leftChild[i], leftChild, rightChild)&&dfs(rightChild[i], leftChild, rightChild)}}
classSolution{funcvalidateBinaryTreeNodes(_ n:Int,_ leftChild:[Int],_ rightChild:[Int])->Bool{var hasParent =Set<Int>()for c in leftChild where c !=-1{ hasParent.insert(c)}for c in rightChild where c !=-1{ hasParent.insert(c)}if hasParent.count == n {returnfalse}var root =-1for i in0..<n {if!hasParent.contains(i){
root = i
break}}var visit =Set<Int>()funcdfs(_ i:Int)->Bool{if i ==-1{returntrue}if visit.contains(i){returnfalse}
visit.insert(i)returndfs(leftChild[i])&&dfs(rightChild[i])}returndfs(root)&& visit.count == n
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
2. Breadth First Search
Intuition
Instead of using recursion, we can use BFS to traverse the tree level by level. The approach uses indegree counting: in a valid binary tree, every node except the root has exactly one incoming edge (one parent). By tracking indegrees, we can detect nodes with multiple parents and identify the unique root.
Algorithm
Create an indegree array and count incoming edges for each node from both leftChild and rightChild arrays.
If any node has an indegree greater than 1 (multiple parents), return false immediately.
Find the root by locating the node with indegree == 0. If there are multiple such nodes or none, return false.
Perform bfs starting from the root using a queue, counting visited nodes.
For each node dequeued, add its valid children to the queue.
After bfs completes, return true only if the count of visited nodes equals n.
This approach combines the indegree validation from BFS with an iterative stack-based traversal instead of recursion. Using a stack avoids potential stack overflow issues for deep trees while maintaining the same logical flow as recursive DFS.
Algorithm
Create an indegree array and count incoming edges for each node.
Return false if any node has more than one parent.
Find the unique root node with indegree == 0. Return false if no root exists or multiple roots exist.
Initialize a stack with the root node and a counter for visited nodes.
Pop nodes from the stack, increment the counter, and push valid children onto the stack.
After the stack is empty, verify that the count equals n to confirm all nodes are reachable.
Union-Find provides an elegant way to detect cycles and verify connectivity. The key insight is that in a valid tree, connecting a parent to a child should always merge two separate components. If the child already has a parent (its root is not itself) or connecting them would create a cycle (same root), the structure is invalid.
Algorithm
Initialize a DSU structure where each node is its own parent, with n separate components.
For each parent node, attempt to union it with its left and right children.
During union, check that the child's root equals itself (meaning it has no parent yet) and that the parent and child are not already in the same component.
If the union fails these checks, return false as it indicates multiple parents or a cycle.
For each successful union, decrement the component count.
After processing all edges, return true only if there is exactly one connected component.
A common mistake is assuming node 0 is always the root of the tree. The root is the node with no incoming edges (no parent), which could be any node from 0 to n-1. You must explicitly find the root by identifying which node never appears in leftChild or rightChild arrays.
Forgetting to Check for Multiple Roots
A valid binary tree has exactly one root. If multiple nodes have no parent (indegree of 0), the structure is a forest, not a single tree. Always verify that exactly one node qualifies as the root before proceeding with traversal.
Not Verifying All Nodes Are Reachable
Even if you find a valid root and detect no cycles, you must confirm that all n nodes are reachable from the root. Disconnected components would mean some nodes are not part of the tree rooted at the identified root. After traversal, verify the count of visited nodes equals n.