Follow Suryakant Chaurasiya
Tree - collection of tree- nodes
Maximum depth of n-ary tree
Idea is same as previous problem, only implementation changes
• Recursively visit the second
child.
• …..
Approach: • Recursively visit the second
last child.
The inorder traversal of an N-ary tree is
• Print the data in the node.
defined as visiting all the children except the
• Recursively visit the last child.
last then the root and finally the last child
• Repeat the above steps till all
recursively.
the nodes are visited.
• Recursively visit the first child. void inorder(Node *node)
{ if (node == NULL)
return;
// Total children count int total =
node->length;
// All the children except the last for (int i
= 0; i < total - 1; i++) inorder(node-
>children[i]);
// Print the current node's data cout<< node-
>data << " ";
// Last child inorder(node->children[total -
1]);
}
stoi() can take upto three parameters, the second parameter is for starting index
and third parameter is for base of input number.