2. Take input levelwise #

Created Monday 27 January 2020

code: template TreeNode *levelwise_Input() { //make the root cout << "Data for root node: "; int data; cin >> data;

TreeNode *root = new TreeNode(data);

queue<TreeNode *> nodesPending; nodesPending.push(root); // obviously pending

while (nodesPending.size() != 0) { data = nodesPending.front()->data; // cureent root’s data cout << "Number of children for " << data << " : "; int n; cin >> n; for (int i = 0; i < n; i++) { cout << "Data for " << data << "'s " << i << "th child: "; TreeNode child = new TreeNode; cin >> child->data; nodesPending.front()->children.push_back(child); nodesPending.push(child); } nodesPending.pop(); } return root; // When you are a leaf / Insights: Takes in a BFS way, is an implicit queue. Easy to use. Maid: Do things in the order of arrival, queue Description: Goes to a node, takes all data for its children. Then starts from the first child. When that is over goes to the second child. Ans so on. */ }