The TransCompilator

Solved LeetCode program · 801 of 1,000

#1038 · Binary Search Tree to Greater Sum Tree

Medium Depth-First Search chunk-17.md

Original English source

Keep a shared values holding 4, 1, 6, 0, 2, 5, and 7. Keep a shared lefts holding 1, 3, 5, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, -1, -1, and -1. Keep a shared running of 0.

To revise from node (a whole number). If node is less than 0, return. Revise from item node of rights. Increase running by item node of values. Set item node of values to running. Revise from item node of lefts.

Revise from 0. 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. Show item node of values. 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. If right is at least 0, Place right into queue, add 1 to count.

Open in Playground Browse all 1,000 problems

Expected stdout

22
25
13
25
24
18
7