We have a node tree from a system, the Node model is like
Public class Node {
public string Name { get; set; }
public bool HasChildren { get; set; }
public List<Node> Children { get; set; }
}
So, based on the Node model, the JSON of the node tree will be something like below:
{
"Name": "Root",
"HasChildren": true,
"Children": [
{
"Name": "Node-1",
"HasChildren": true,
"Children": [
{
"Name": "Node-1-1",
"HasChildren": false,
"Children": []
}
]
},
{
"Name": "Node-2",
"HasChildren": true,
"Children": [
{
"Name": "Node-2-1",
"HasChildren": false,
"Children": [
{
"Name": "Node-2-1-1",
"HasChildren": false,
"Children": []
}
]
},
{
"Name": "Node-2-2",
"HasChildren": false,
"Children": [
{
"Name": "Node-2-2-1",
"HasChildren": false,
"Children": []
}
]
}
]
}
...
]
}
Now, I'd like to save every node in a flattened array or list. To traverse all the nodes, I know I can recursively get them. But, using recursion causes a big usage of memory.
Is there a way of using the loop logic, like while, for, or foreach logic to do the same?