The TransCompilator

Solved LeetCode program · 110 of 1,000

#0110 · Balanced Binary Tree

Easy Depth-First Search chunk-03.md

Original English source

Keep a shared values holding 3, 9, 20, 15, and 7. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, 4, -1, and -1.

The last is the last position of values. The size is last plus 1. The heights is a list of size zeros.
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.

The good is 1.
For every node from 0 to last.
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 difference is leftheight minus rightheight. If difference is more than 1, set good to 0. If difference is less than -1, set good to 0.

If good is 1, show true, otherwise show false.

Open in Playground Browse all 1,000 problems

Expected stdout

true