Member-only story

Leetcode 1361 Validate Binary Tree Nodes

Pierre-Marie Poitevin
2 min readFeb 25, 2020

--

All explained in the code below

class Solution {
public boolean validateBinaryTreeNodes(
int n, int[] leftChild, int[] rightChild) {
// Rules for a tree
// - each node that is not the root is exactly one time a
// left or right child (should be in right or left)
// - the rooth is not in left child or right child
// - all nodes should be accessible from the root
// all these conditions are necessare and sufficient (it is
// possible to show it)
// From all these we get the following algorithm:
// 1. Find the root and make sure each other node appears
// only once the leftChild or rightChild
// 2. bfs from the root
// return true if the dfs visited all nodes
Set<Integer> nodes = new HashSet<>();
for (int i = 0; i < n; i++) {
nodes.add(i);
}
for (int val : leftChild) {
if (val != -1) {
if (!nodes.contains(val)) {
// appears many times
return false;
}
nodes.remove(val);
}
}
for (int val : rightChild) {
if (val != -1) {
if (!nodes.contains(val)) {
// appears many times
return false;
}
nodes.remove(val);
}
}
if (nodes.size() != 1) {
return false;
}
int root = nodes.iterator().next()…

--

--

Pierre-Marie Poitevin
Pierre-Marie Poitevin

No responses yet