call(back)
Algorithms & Data StructureseasySign in to star problems

Pre-Order Traversal Skipping Marked Nodes

Return the pre-order traversal (node, left subtree, right subtree) of a binary tree, except that a node whose value is in marked is skipped together with its entire subtree. Trees arrive in level order with null for missing children.

preorderSkipping([1, 2, 3, 4, 5, 6, 7], [2])     ->  [1, 3, 6, 7]
preorderSkipping([1, 2, 3, 4, 5, 6, 7], [4, 7])  ->  [1, 2, 5, 3, 6]
preorderSkipping([1, 2, 3], [1])                 ->  []

Hints

Hint 1

Iterative pre-order: pop a node, skip it entirely if marked, otherwise record it and push the right child then the left child.

Asked at