← 返回 snowflake 的题目列表Pre-order Traversal Excluding Invalid Nodes
类型:online_judge
Problem Statement
Given a list of tree edges edges and a list of invalid nodes invalid_nodes, implement a function that returns the pre-order traversal of the tree, excluding the invalid nodes.
Function signature: def pre_order_traversal_excluding_invalid_nodes(edges: List[Tuple[int, int]], invalid_nodes: Set[int]) -> List[int]:
Input
edges: A list representing the tree edges. Each edge is a tuple of the format (parent, child).
invalid_nodes: A set containing the IDs of invalid nodes.
Output
Return the pre-order traversal of the tree, excluding the invalid nodes.
Example
Input
edges = [(1, 2), (1, 3), (2, 4), (2, 5)]
invalid_nodes = {2}
Output
[1, 3]
Notes
Node IDs are integers and unique.
The tree is a directed tree rooted at node ID 1.
The provided edges guarantee a valid tree structure.
Example
Input
[(1, 2), (1, 3), (2, 4), (2, 5)], {2}