Creating tree from string

Given the string string str = "(5((9()),(1((4()),(12()),(42()))))"; my goal is to create tree where each node can have >=0 children.In this example the root is 5 and the children and their children follow in the defined string above. () means no child.

What I have tried : I suppose whenever there is ( then there is ) so we remove them and go into recursion however that doesn't work in the case when we reach a string for example (5),(4) , because we have 2 children and it will be incorrect to go in depth by removing the first and the last bracket in this string. I suppose there will be some solution with a stack but I cannot figure it out. Any suggestions how to solve this?

by tree I meant to create a tree using the following structure:

struct Node
{
int data;
vector<Node> children;
}


from Recent Questions - Stack Overflow https://ift.tt/3hbvZL5
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations