← 返回 waymo 的题目列表Filter an N-ary Tree and Render It with Indentation
类型:online_judge
Problem: Filter an N-ary Tree and Render It with Indentation
You are given an N-ary tree. Each node has:
value: a string
children: a list of child nodes
You are also given a string query. Implement a program that:
Removes the entire subtree rooted at any node whose value contains query as a substring.
Returns the filtered forest. If the root is removed, the result may be empty.
Renders the remaining tree using preorder traversal.
Uses two spaces per indentation level.
Input Format
Read a JSON object from standard input:
{
"query": "bad",
"tree": {
"value": "root",
"children": [
{"value": "keep", "children": []},
{"value": "bad-node", "children": [
{"value": "child", "children": []}
]}
]
}
}
Output Format
Print the rendered filtered tree, one node per line.
If the entire tree is removed, print nothing.
Constraints
Number of nodes n: 0 <= n <= 10^5
value.length <= 100
query.length <= 100
Maximum tree depth is at most 10^5
Substring matching is case-sensitive
Example
Input:
{"query":"bad","tree":{"value":"root","children":[{"value":"keep","children":[]},{"value":"bad-node","children":[{"value":"child","children":[]}]}]}}
Output:
root
keep
Example
Input
{"query":"bad","tree":{"value":"root","children":[{"value":"keep","children":[]},{"value":"bad-node","children":[{"value":"child","children":[]}]}]}}
Output
root
keep