The TransCompilator

Solved LeetCode program · 205 of 1,000

#0236 · Lowest Common Ancestor of a Binary Tree

Medium Depth-First Search chunk-05.md

Original English source

Keep a shared values holding 3, 5, 1, 6, 2, 0, 8, 7, and 4. Keep a shared lefts holding 1, 3, 5, -1, 7, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, 8, -1, -1, -1, and -1. The last is 8. The parents is a list of 9 zeros. Set item 0 of parents to -1. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. The left is item node of lefts. The right is item node of rights. If left is at least 0, Place left into queue, add 1 to count, set item left of parents to node. If right is at least 0, Place right into queue, add 1 to count, set item right of parents to node.

The firstnode is 0. The secondnode is 0.
For every spot from 0 to last.
If item spot of values is 5, set firstnode to spot. If item spot of values is 1, set secondnode to spot.

The marks is a list of 9 zeros. The walker is firstnode.
Repeat while walker is at least 0.
Set item walker of marks to 1. Set walker to item walker of parents.

The seeker is secondnode.
Repeat while item seeker of marks is 0.
Set seeker to item seeker of parents.

Show item seeker of values.

Open in Playground Browse all 1,000 problems

Expected stdout

3