Take Input Level Wise

 TreeNode<int>* takeInputLevel(){

    int rootData;

    cout<<"Enter Data: "<<endl;

    cin>>rootData;

    TreeNode <int>* root = new TreeNode<int>(rootData);

    queue<TreeNode<int>*> pendingNode;//queue

    pendingNode.push(root);

    while(pendingNode.size()!=0){

        TreeNode<int>*front = pendingNode.front();

        pendingNode.pop();

        cout<<"Enter the number of children of "<<front->data<<endl;

        int childnum;

        cin>>childnum;

        for (int i=0;i<childnum;i++){

            cout<<"Enter the "<<i<<"th data of root"<<front->data<<endl;

            int childata;

            cin>>childata;

            TreeNode <int>* child = new TreeNode<int>(childata);

            pendingNode.push(child);

            front->children.push_back(child);

        }

    }

    return root;

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings