Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//main.cpp #include #include using namespace std; // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; }; vector >

image text in transcribed

//main.cpp

#include

#include

using namespace std;

// Definition for a binary tree node.

struct TreeNode {

int val;

TreeNode *left;

TreeNode *right;

};

vector> printFromButtom(TreeNode* root) {

// your code here

return vector>();

}

void printAns(vector> v) {

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> ans = printFromButtom(&n_0);

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> ans2 = printFromButtom(&n_00);

cout

printAns(ans2);

return 0;

}

The Problem Complete the Buttom ction that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions