to_delete
list into a set for efficient deletion checks.walk
that accepts a node and a boolean indicating if the parent exists.None
, return None
.to_delete
set, recursively call walk
on its children with parent_exist
set to False
, and return None
.to_delete
set and the parent does not exist, add the current node to the result list.walk
on the left and right children of the current node with parent_exist
set to True
.None
.walk
function with the root node and parent_exist
set to False
.