← 返回 xai 的题目列表Flatten a Nested Python Structure (lists/dicts/tuples) into a List of Integers
类型:online_judge
Problem: Flatten a Nested Structure
When working with hierarchical data, you may need to convert a nested structure into a flat list.
Input
Given a nested Python-like data structure structure whose nodes can be:
list
tuple
dict
Constraint: all leaf nodes (innermost values) are guaranteed to be integers (int).
Output
Return a 1D list flat_list containing all integers from structure in traversal order.
Traversal / Ordering Rules
For list / tuple: traverse elements from left to right.
For dict: traverse its values following the dictionary’s iteration order of keys (Python insertion/iteration order).
Example
Input:
{'a': [1, 2, 3], 'b': {'c': [{'d': 4}], 'e': 5}}
Output:
[1, 2, 3, 4, 5]
Constraints
Nesting depth D: 1 ≤ D ≤ 10^4
Number of leaf integers N: 1 ≤ N ≤ 2 * 10^5
Requirements
Must handle very deep nesting correctly.
Target time complexity: O(N), where N is the number of leaf integers.
Example
Input
{'a': [1, 2, 3], 'b': {'c': [{'d': 4}], 'e': 5}}
Output
[1, 2, 3, 4, 5]