<\/span><\/h2>\n\n\n\nA level-order traversal, also known as a breadth-first search, visits each level of a tree’s nodes from left to right, top to bottom. You are given a pointer, root<\/strong><\/em>, pointing to the root of a binary search tree. Complete the levelOrder<\/em> function provided in your editor so that it prints the level-order traversal of the binary search tree.<\/p>\n\n\n\nHint:<\/strong> You’ll find a queue helpful in completing this challenge.<\/p>\n\n\n\nFunction Description<\/strong><\/p>\n\n\n\nComplete the levelOrder<\/em> function in the editor below.<\/p>\n\n\n\nlevelOrder<\/em> has the following parameter:
– Node pointer root<\/em>: a reference to the root of the tree<\/p>\n\n\n\nPrints<\/strong>
– Print node.data items as space-separated line of integers. No return value is expected.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe locked stub code in your editor reads the following inputs and assembles them into a BST:
The first line contains an integer, T<\/em><\/strong> (the number of test cases).
The T<\/em><\/strong> subsequent lines each contain an integer, data<\/em><\/strong>, denoting the value of an element that must be added to the BST.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= N<\/em> <= 20<\/strong><\/li>
- 1 <= node.data<\/em>[i<\/em>] <= 100<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the data<\/strong><\/em> value of each node in the tree’s level-order traversal as a single line of N<\/em><\/strong> space-separated integers.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n6\n3\n5\n4\n7\n2\n1<\/code><\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\n3 2 5 1 4 7 <\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nWe traverse each level of the tree from the root downward, and we process the nodes at each level from left to right. The resulting level-order traversal is 3 = 2 = 5 = 1 = 4 = 7<\/strong>, and we print these data values as a single line of space-separated integers.<\/p>\n\n\n\n<\/span>Solution – Day 23: BST Level-Order Traversal<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <cstddef>\n#include <queue>\n#include <string>\n#include <cstdlib>\n\nusing namespace std;\t\nclass Node{\n public:\n int data;\n Node *left,*right;\n Node(int d){\n data=d;\n left=right=NULL;\n }\n};\nclass Solution{\n public:\n \t\tNode* insert(Node* root, int data){\n if(root==NULL){\n return new Node(data);\n }\n else{\n Node* cur;\n if(data<=root->data){\n cur=insert(root->left,data);\n root->left=cur;\n }\n else{\n cur=insert(root->right,data);\n root->right=cur;\n } \n return root;\n }\n }\n \n void levelOrder(Node * root){\n queue<Node *> q;\n Node* n = root;\n \n while(n != NULL){\n cout << n->data << ' ';\n \n if( n->left != NULL ) q.push(n->left);\n if( n->right != NULL ) q.push(n->right);\n if( !q.empty() ) {\n n = q.front();\n q.pop();\n } else {\n n = NULL;\n }\n }\n}\n};\/\/End of Solution\nint main(){\n Solution myTree;\n Node* root=NULL;\n int T,data;\n cin>>T;\n while(T-->0){\n cin>>data;\n root= myTree.insert(root,data);\n }\n myTree.levelOrder(root);\n return 0;\n}<\/pre>\n\n\n\n