The TransCompilator

Solved LeetCode program · 404 of 1,000

#0543 · Diameter of Binary Tree

Easy Depth-First Search chunk-09.md

Original English source

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

Keep a shared longest of 0.

To deepen from node (a whole number). If node is less than 0, return. The leftnode is item node of lefts. The rightnode is item node of rights. The leftheight is 0. The rightheight is 0. If leftnode is at least 0, deepen from leftnode. If leftnode is at least 0, set leftheight to item leftnode of heights. If rightnode is at least 0, deepen from rightnode. If rightnode is at least 0, set rightheight to item rightnode of heights. The through is leftheight plus rightheight. If through is more than longest, set longest to through. The taller is leftheight. If rightheight is more than taller, set taller to rightheight. Set item node of heights to taller plus 1.

Deepen from 0. Show longest.

Open in Playground Browse all 1,000 problems

Expected stdout

3