{"id":"b5af1f40-cd83-4228-8ffd-9ebb2b2ae9c6","name":"Right View Of A Binary Tree","description":"1. Given a Binary Tree, print Right view of it. \r\n2. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nclass levelPair\r\n{\r\npublic:\r\n TreeNode *par = nullptr;\r\n int lb = -(int)1e8;\r\n int rb = (int)1e8;\r\n\r\n levelPair()\r\n {\r\n }\r\n\r\n levelPair(TreeNode *par, int lb, int rb)\r\n {\r\n this->par = par;\r\n this->lb = lb;\r\n this->rb = rb;\r\n }\r\n};\r\n\r\nvector<int> rightView(TreeNode *root)\r\n{\r\n}\r\n\r\n// input_section=================================================\r\n\r\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n\r\n vector<int> ans = rightView(root);\r\n for (int i : ans)\r\n cout << i << endl;\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ArrayList<Integer> rightView(TreeNode root) {\r\n return null;\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n\r\n ArrayList<Integer> ans = rightView(root);\r\n for(Integer i : ans) System.out.println(i); \r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"15\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1","sampleOutput":"1\r\n1\r\n1\r\n1\r\n1\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"b7c20dfd-806d-438d-b4b1-2176aca68e2f","name":"Right View Of A Binary Tree","slug":"right-view-of-a-binary-tree","type":1}],"next":{"id":"b1a2d1f8-d242-45bd-9021-a90992c37451","name":"Cycle Node In Linkedlist","type":1,"slug":"cycle-node-in-linkedlist"},"prev":{"id":"fc847e86-a947-4043-8914-b38f3c7f57ba","name":"Left View Of A Binary Tree","type":1,"slug":"left-view-of-a-binary-tree"}}}

Right View Of A Binary Tree

1. Given a Binary Tree, print Right view of it. 2. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.

{"id":"b5af1f40-cd83-4228-8ffd-9ebb2b2ae9c6","name":"Right View Of A Binary Tree","description":"1. Given a Binary Tree, print Right view of it. \r\n2. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you.\r\n","constraints":"0 &lt;= Number of Nodes &lt;= 10^5\r\n-1000 &lt;= value of Node data &lt;= 1000\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <queue>\r\nusing namespace std;\r\n\r\nclass TreeNode\r\n{\r\npublic:\r\n int val = 0;\r\n TreeNode *left = nullptr;\r\n TreeNode *right = nullptr;\r\n\r\n TreeNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nclass levelPair\r\n{\r\npublic:\r\n TreeNode *par = nullptr;\r\n int lb = -(int)1e8;\r\n int rb = (int)1e8;\r\n\r\n levelPair()\r\n {\r\n }\r\n\r\n levelPair(TreeNode *par, int lb, int rb)\r\n {\r\n this->par = par;\r\n this->lb = lb;\r\n this->rb = rb;\r\n }\r\n};\r\n\r\nvector<int> rightView(TreeNode *root)\r\n{\r\n}\r\n\r\n// input_section=================================================\r\n\r\nTreeNode *createTree(vector<int> &arr, vector<int> &IDX)\r\n{\r\n\r\n if (IDX[0] > arr.size() || arr[IDX[0]] == -1)\r\n {\r\n IDX[0]++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode *node = new TreeNode(arr[IDX[0]++]);\r\n node->left = createTree(arr, IDX);\r\n node->right = createTree(arr, IDX);\r\n\r\n return node;\r\n}\r\n\r\nvoid solve()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n {\r\n cin >> arr[i];\r\n }\r\n\r\n vector<int> IDX(1, 0);\r\n TreeNode *root = createTree(arr, IDX);\r\n\r\n vector<int> ans = rightView(root);\r\n for (int i : ans)\r\n cout << i << endl;\r\n}\r\n\r\nint main()\r\n{\r\n solve();\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class TreeNode {\r\n int val = 0;\r\n TreeNode left = null;\r\n TreeNode right = null;\r\n\r\n TreeNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ArrayList<Integer> rightView(TreeNode root) {\r\n return null;\r\n\r\n }\r\n\r\n // input_section=================================================\r\n\r\n public static TreeNode createTree(int[] arr, int[] IDX) {\r\n if (IDX[0] > arr.length || arr[IDX[0]] == -1) {\r\n IDX[0]++;\r\n return null;\r\n }\r\n TreeNode node = new TreeNode(arr[IDX[0]++]);\r\n node.left = createTree(arr, IDX);\r\n node.right = createTree(arr, IDX);\r\n\r\n return node;\r\n }\r\n\r\n public static void solve() {\r\n int n = scn.nextInt();\r\n int[] arr = new int[n];\r\n for (int i = 0; i < n; i++)\r\n arr[i] = scn.nextInt();\r\n\r\n int[] IDX = new int[1];\r\n TreeNode root = createTree(arr, IDX);\r\n\r\n ArrayList<Integer> ans = rightView(root);\r\n for(Integer i : ans) System.out.println(i); \r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"15\r\n1\r\n1\r\n-1\r\n1\r\n1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1\r\n1\r\n-1\r\n-1","sampleOutput":"1\r\n1\r\n1\r\n1\r\n1\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"b7c20dfd-806d-438d-b4b1-2176aca68e2f","name":"Right View Of A Binary Tree","slug":"right-view-of-a-binary-tree","type":1}],"next":{"id":"b1a2d1f8-d242-45bd-9021-a90992c37451","name":"Cycle Node In Linkedlist","type":1,"slug":"cycle-node-in-linkedlist"},"prev":{"id":"fc847e86-a947-4043-8914-b38f3c7f57ba","name":"Left View Of A Binary Tree","type":1,"slug":"left-view-of-a-binary-tree"}}}
plane

Editor


Loading...

Right View Of A Binary Tree

easy

1. Given a Binary Tree, print Right view of it. 2. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side.

Constraints

0 <= Number of Nodes <= 10^5 -1000 <= value of Node data <= 1000

Format

Input

Input is managed for you.

Output

Output is managed for you.

Example

Sample Input

15 1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 1 -1 -1

Sample Output

1 1 1 1 1

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode