Print Level Wise
N:x1,x2,x3,...,xn
where, N is data of any node present in the generic tree. x1, x2, x3, ...., xn are the children of node N. Note that there is no space in between.
The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space.
The first and only line of output contains the elements of the tree in level wise order, as described in the task.
Time Limit: 1 sec
10 3 20 30 40 2 40 50 0 0 0 0
10:20,30,40
20:40,50
30:
40:
40:
50:
/************************************************************ Following is the structure for the TreeNode class
template <typename T> class TreeNode { public: T data; vector<TreeNode<T>*> children; TreeNode(T data) { this->data = data; } ~TreeNode() { for (int i = 0; i < children.size(); i++) { delete children[i]; } } };
************************************************************/
void printLevelWise(TreeNode<int>* root) { // Write your code here if(root==NULL) return; queue<TreeNode<int>*>pendingnode; pendingnode.push(root); while(!pendingnode.empty()){ TreeNode<int>* front=pendingnode.front(); pendingnode.pop(); cout<<front->data<<":"; for(int i=0;i<front->children.size();i++){ if(i==front->children.size()-1) cout<<front->children[i]->data; else cout<<front->children[i]->data<<","; pendingnode.push(front->children[i]); } cout<<endl; } return; }
Comments
Post a Comment