The TransCompilator

Solved LeetCode program · 949 of 1,000

#1315 · Sum of Nodes with Even-Valued Grandparent

Medium Depth-First Search chunk-19.md

Original English source

Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared total of 0. Place 2 into values. Place 4 into values. Place 5 into values. Place 1 into values. Place 3 into values. Place 6 into values. Place 7 into values. Place 1 into lefts. Place 3 into lefts. Place 5 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place 2 into rights. Place 4 into rights. Place 6 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights.

To walk from node (a whole number) with parent (a whole number) and grand (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. The piece is item node of values. Walk from left with node and parent. Walk from right with node and parent. If grand is at least 0 and grand modulo 2 is 0, add piece to total.

Walk from 0 with -1 and -1. Show total.

Open in Playground Browse all 1,000 problems

Expected stdout

17