Question: in C++ Problem Statement Given the root node to a non-empty Binary Search Tree, write a function that returns a vector whose values are the


in C++
Problem Statement Given the root node to a non-empty Binary Search Tree, write a function that returns a vector whose values are the sums of all the Treenode values in each level of the BST. There should be as many values in the vector as levels in the tree, with the first value in the vector corresponding to the topmost level in the tree. The function will have the following signature: vector int> levelOrder (TreeNode* root) We have defined the following TreeNode C++ class for you: class TreeNode { public: int val; TreeNode *left; TreeNode *right; TreeNode) : val(o), left(nullptr), right(nullptr) {} TreeNode (int x) : val(x), left(nullptr), right(nullptr) {} TreeNode (int x, TreeNode *left, TreeNode tright) : val(x), left(left), right(right) {) }; Example 1: 9 7 12 1 8 10 4 Input: [9,12,7,1,4,8,10] Output: 9 19 19 4 Test Cases The items in the input are inserted into the Binary Search Tree from left to right. We will loop through the vector to print out the sum for each level in the main method. Note You are only required to return a vector with the sums of each level's nodes. You should not print anything out in the levelOrder method. We will print out the items you store in the vector in the main method. Difficulty Hard (20-25 mins) Author: Lisha Zhou, Date Created: 20 May 2020, Last Modified: 20 May 2020 Sample Input: [3,9, 8,2,5,4,11] Sample Output: 3 11 19 5 4 Correct answer from 8 learners Total 100% of tries are correct Write a program, test using stdin stdout Time limit: 5 seconds Memory limit: 256 MB 1 vector int> levelOrder (TreeNode* root) { 2 // your code 3}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
