Validate Binary Tree Nodes

medium tree dfs bfs graph union find binary tree

Problem

n nodes labeled 0..n-1; leftChild[i] and rightChild[i] give the children of node i (-1 if none). Return true if these form exactly one valid binary tree.

Inputn = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Outputtrue
Node 0 is root with children 1 and 2; node 2 has child 3.

def validate_binary_tree_nodes(n, leftChild, rightChild):
    indeg = [0] * n
    for c in leftChild + rightChild:
        if c != -1:
            indeg[c] += 1
            if indeg[c] > 1: return False
    roots = [i for i in range(n) if indeg[i] == 0]
    if len(roots) != 1: return False
    seen, stk = {roots[0]}, [roots[0]]
    while stk:
        u = stk.pop()
        for c in (leftChild[u], rightChild[u]):
            if c != -1:
                if c in seen: return False
                seen.add(c); stk.append(c)
    return len(seen) == n
function validateBinaryTreeNodes(n, leftChild, rightChild) {
  const indeg = new Array(n).fill(0);
  for (const c of leftChild.concat(rightChild)) {
    if (c !== -1) { indeg[c]++; if (indeg[c] > 1) return false; }
  }
  let root = -1;
  for (let i = 0; i < n; i++) if (indeg[i] === 0) { if (root !== -1) return false; root = i; }
  if (root === -1) return false;
  const seen = new Set([root]); const stk = [root];
  while (stk.length) {
    const u = stk.pop();
    for (const c of [leftChild[u], rightChild[u]]) {
      if (c !== -1) { if (seen.has(c)) return false; seen.add(c); stk.push(c); }
    }
  }
  return seen.size === n;
}
class Solution {
    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        int[] indeg = new int[n];
        for (int c : leftChild) if (c != -1 && ++indeg[c] > 1) return false;
        for (int c : rightChild) if (c != -1 && ++indeg[c] > 1) return false;
        int root = -1;
        for (int i = 0; i < n; i++) if (indeg[i] == 0) { if (root != -1) return false; root = i; }
        if (root == -1) return false;
        Set seen = new HashSet<>(); seen.add(root);
        Deque st = new ArrayDeque<>(); st.push(root);
        while (!st.isEmpty()) {
            int u = st.pop();
            for (int c : new int[]{leftChild[u], rightChild[u]}) if (c != -1) {
                if (!seen.add(c)) return false;
                st.push(c);
            }
        }
        return seen.size() == n;
    }
}
bool validateBinaryTreeNodes(int n, vector& leftChild, vector& rightChild) {
    vector indeg(n, 0);
    for (int c : leftChild) if (c != -1 && ++indeg[c] > 1) return false;
    for (int c : rightChild) if (c != -1 && ++indeg[c] > 1) return false;
    int root = -1;
    for (int i = 0; i < n; i++) if (indeg[i] == 0) { if (root != -1) return false; root = i; }
    if (root == -1) return false;
    vector seen(n, false); seen[root] = true;
    stack st; st.push(root);
    int cnt = 1;
    while (!st.empty()) {
        int u = st.top(); st.pop();
        for (int c : {leftChild[u], rightChild[u]}) if (c != -1) {
            if (seen[c]) return false;
            seen[c] = true; cnt++; st.push(c);
        }
    }
    return cnt == n;
}
Time: O(n) Space: O(n)