Back to problems

Preorder Traversal Without Invalid Nodes

Algorithm · Snowflake · Easy

You are given a rooted tree containing n nodes numbered from 0 through n - 1. The tree is described by directed edges, with edges[i] = [parent, child]. You also receive the tree's root and an array named invalid that lists nodes marked as invalid. Produce the tree's preorder traversal after leaving invalid nodes out of the returned sequence. Traversal rules: Record each node before processing any of its children. Process children according to their order in edges. An invalid…

Checking your access…