Question: You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined
You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left.
We have defined the following node C++ Node class for you:
class Node { public: int name; Node* left = NULL; Node* right = NULL; }; Function to code:
void printLeaves(Node* root);
The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access to the root of the constructed Binary Search Tree. The output are the leaves separated by one space " " printed from right to left.
Examples:-
1. Numbers: - 5 2 15 16 9 11 14
Tree:- 5
/ \
2 15
/ \
9 16
\
11
\
14
Output: - 16 14 2
2. Numbers: - 5 2 15
Tree:- 5
/ \
2 15
Output: - 15 2
Sample Input 1:
5 2 15 16 9 11 14
Sample Output 1:
16 14 2
Sample Input 2:
5 2 15
Sample Output 2:
15 2
Sample Input 3:
1
Sample Output 3:
1
//code template
void printLeaves(Node* root) { //your code here }
part b;
Write the code complexity in terms of Big O notation for your solution to the above code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
