Question
//main.cpp #include #include using namespace std; // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; }; vector >
//main.cpp
#include
#include
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};
vector
// your code here
return vector
}
void printAns(vector
cout
for(int i = 0; i cout for(int j = 0; j cout if(j cout } } cout } cout } int main() { /* * Example 1: * 3 * / \ * 9 20 * / \ * 15 7 *return: * [ * [15,7], * [9,20], * [3] * ] */ TreeNode n_0, n_1, n_2, n_3, n_4; n_0.val = 3; n_1.val = 9; n_2.val = 20; n_3.val = 15; n_4.val = 7; n_0.left = &n_1; n_0.right = &n_2; n_2.left = &n_3; n_2.right = &n_4; n_1.left = NULL; n_1.right = NULL; n_3.left = NULL; n_3.right = NULL; n_4.left = NULL; n_4.right = NULL; vector cout printAns(ans); /* * Example 2 * 10 * / \ * 8 13 * / \ \ * 3 9 16 * Return: * [ * [3, 9, 16], * [8, 13], * [10] * ] */ TreeNode n_00, n_01, n_02, n_03, n_04, n_05; n_00.val = 10; n_01.val = 8; n_02.val = 13; n_03.val = 3; n_04.val = 9; n_05.val = 16; n_00.left = &n_01; n_00.right = &n_02; n_01.left = &n_03; n_01.right = &n_04; n_02.left = NULL; n_02.right = &n_05; n_03.left = NULL; n_04.left = NULL; n_05.left = NULL; n_03.right = NULL; n_04.right = NULL; n_05.right = NULL; vector cout printAns(ans2); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started