The TransCompilator

Solved LeetCode program · 985 of 1,000

#1361 · Validate Binary Tree Nodes

Medium Depth-First Search chunk-20.md

Original English source

Keep a shared lefts holding 1, -1, 3, and -1. Keep a shared rights holding 2, -1, -1, and -1. Keep a shared indegree as an empty list of whole numbers.
For every node from 0 to 3.
Place 0 into indegree.

Keep a shared bad of 0.

To note from child (a whole number). If child is -1, return. If item child of indegree is more than 0, set bad to 1. Increase item child of indegree by 1.

For each child in lefts.
Note from child.

For each child in rights.
Note from child.

The root is -1. The roots is 0.
For every node from 0 to 3.
If item node of indegree is 0, set root to node, add 1 to roots.

Keep a shared visited as an empty list of whole numbers.
For every node from 0 to 3.
Place 0 into visited.

Keep a shared reached of 0.

To walk from node (a whole number). If node is less than 0, return. If item node of visited is 1, return. Set item node of visited to 1. Add 1 to reached. Walk from item node of lefts. Walk from item node of rights.

If roots is 1, Walk from root. If bad is 0 and roots is 1 and reached is 4, show true, otherwise show false.

Open in Playground Browse all 1,000 problems

Expected stdout

true