The TransCompilator

Solved LeetCode program · 841 of 1,000

#1123 · Lowest Common Ancestor of Deepest Leaves

Medium Depth-First Search chunk-17.md

Original English source

Keep a shared values holding 3, 5, 1, 6, 2, 0, 8, -1, -1, 7, and 4. Keep a shared lefts holding 1, 3, 5, -1, 9, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, 10, -1, -1, -1, -1, -1, and -1. Keep a shared depths as an empty list of whole numbers.
For every node from 0 to 10.
Place 0 into depths.

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

To score from node (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. Score from left. Score from right. The leftdepth is -1. If left is at least 0, set leftdepth to item left of depths. The rightdepth is -1. If right is at least 0, set rightdepth to item right of depths. If leftdepth is more than rightdepth, Set item node of depths to leftdepth plus 1, Set item node of ancestors to item left of ancestors. If rightdepth is more than leftdepth, Set item node of depths to rightdepth plus 1, Set item node of ancestors to item right of ancestors. If leftdepth is rightdepth, Set item node of depths to leftdepth plus 1, Set item node of ancestors to node.

Score from 0. The answer is item 0 of ancestors. The queue is a list of 32 zeros. Set item 0 of queue to answer. The head is 0. The count is 1. The order is an empty list of whole numbers.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. The value is item node of values. Place value into order. The left is item node of lefts. The right is item node of rights. If left is at least 0, Set item count of queue to left, Add 1 to count. If right is at least 0, Set item count of queue to right, Add 1 to count.

The parts is an empty list of text.
For each value in order.
The piece is format whole number value as text. Place piece into parts.

The line is join parts with ",". Show line.

Open in Playground Browse all 1,000 problems

Expected stdout

2,7,4