Validating Binary Tree Nodes | Explanation | Python

Harsh V
2 min readOct 17, 2023

--

17 October Leetcode Problem

Introduction

The provided Python code validates whether a given set of nodes and their connections form a valid binary tree. A binary tree is a tree data structure where each node has at most two children, referred to as the left child and the right child.

Initialization

class Solution(object):
def validateBinaryTreeNodes(self, n, leftChild, rightChild):
roots = set(range(n)) - set(leftChild) - set(rightChild)
if len(roots) != 1:
return False
root, = roots
stk = [root]
lookup = set([root])

The code defines a class Solution with a method validateBinaryTreeNodes that takes three inputs: n (the number of nodes), leftChild (a list representing the left children of nodes), and rightChild (a list representing the right children of nodes).

It initializes a set called roots to contain a range of values from 0 to n - 1, representing all possible node IDs. It then subtracts the sets of left and right children from roots. If there is not exactly one root node (i.e., one node with no parent), the code returns False.

The root node is determined as the single element in the roots set, and it is added to a stack called stk and a set called lookup to track visited nodes.

Validation Using Depth-First Search (DFS)

while stk:
node = stk.pop()
for c in (leftChild[node], rightChild[node]):
if c < 0:
continue
if c in lookup:
return False
lookup.add(c)
stk.append(c)
return len(lookup) == n

The code enters a loop that runs as long as there are nodes in the stk. It performs a depth-first search (DFS) to traverse the tree.

In each iteration, it pops a node from the stack.

It then checks the left and right children of the current node (leftChild[node] and rightChild[node]), and for each child, it performs the following checks:

If the child is less than 0 (i.e., it’s not a valid node ID), it skips it.

If the child is already in the lookup set, indicating that it has been visited before, the code returns False.

Otherwise, it adds the child to the lookup set and pushes it onto the stack for further exploration.

After the DFS traversal, the code checks if the size of the lookup set is equal to n, indicating that all nodes have been visited. If this condition is met, it returns True, indicating that the input nodes and connections form a valid binary tree.

This code efficiently validates whether the given nodes and connections represent a valid binary tree using depth-first search (DFS) for traversal. If the conditions for a binary tree are not met, it returns False.

--

--

No responses yet