The TransCompilator

Solved LeetCode program · 636 of 1,000

#0865 · Smallest Subtree with all the Deepest Nodes

Medium Depth-First Search chunk-13.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 heights as an empty list of whole numbers. Keep a shared answer of 0. Keep a shared order as an empty list of whole numbers.
To descend from node (a whole number). The left is item node of lefts. The right is item node of rights. The leftheight is 0. The rightheight is 0. If left is at least 0, set leftheight to item left of heights. If right is at least 0, set rightheight to item right of heights. If leftheight is equal to rightheight, set answer to node, return. If leftheight is more than rightheight, Descend from left, return. Descend from right.

To gather from node (a whole number). If node is less than 0, return. Place node into order. Gather from item node of lefts. Gather from item node of rights.

The last is the last position of values.
For every node from 0 to last.
Place 0 into heights.

For every step from 0 to last.
The node is last minus step. The leftheight is 0. The rightheight is 0. The left is item node of lefts. The right is item node of rights. If left is at least 0, set leftheight to item left of heights. If right is at least 0, set rightheight to item right of heights. The tall is leftheight. If rightheight is more than tall, set tall to rightheight. Set item node of heights to tall plus 1.

Descend from 0. Gather from answer. For each node in order, show item node of values.

Open in Playground Browse all 1,000 problems

Expected stdout

2
7
4