From 2ddb623b7424922048ce8f2474b1076eddbab8d1 Mon Sep 17 00:00:00 2001 From: Mitul Garg <68656122+gargmitul28@users.noreply.github.com> Date: Wed, 5 Oct 2022 18:55:53 +0530 Subject: [PATCH] Create rightviewofbinarytree.cpp --- Linked_list/rightviewofbinarytree.cpp | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Linked_list/rightviewofbinarytree.cpp diff --git a/Linked_list/rightviewofbinarytree.cpp b/Linked_list/rightviewofbinarytree.cpp new file mode 100644 index 000000000..7aa5c8b0d --- /dev/null +++ b/Linked_list/rightviewofbinarytree.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +struct Node +{ + int data; + struct Node *left, *right; +}; + + +struct Node *newNode(int item) +{ + struct Node *temp = (struct Node *)malloc( + sizeof(struct Node)); + temp->data = item; + temp->left = temp->right = NULL; + return temp; +} + + +void rightViewUtil(struct Node *root, + int level, int *max_level) +{ + // Base Case + if (root == NULL) return; + + + if (*max_level < level) + { + cout << root->data << "\t"; + *max_level = level; + } + + + rightViewUtil(root->right, level + 1, max_level); + rightViewUtil(root->left, level + 1, max_level); +} + + +void rightView(struct Node *root) +{ + int max_level = 0; + rightViewUtil(root, 1, &max_level); +} + + +int main() +{ + struct Node *root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + root->right->left = newNode(6); + root->right->right = newNode(7); + root->right->left->right = newNode(8); + + rightView(root); + + return 0; +}