{"id":"9fd755c9-c1c6-47dc-aa76-c48b9c3c84a8","name":"Node To Root Path Binary Tree","description":"1. Given a Binary Tree, return root To Node path of it. \r\n2. For more Information watch given video link below.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Input 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\nvector<TreeNode*> nodeToRootPath(TreeNode* root, int data)\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 int data;\r\n cin >> data;\r\n vector<TreeNode*> ans = nodeToRootPath(root, data);\r\n if (ans.size() == 0)\r\n cout << endl;\r\n for (TreeNode* node : ans)\r\n {\r\n cout << node->val << \" \";\r\n }\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<TreeNode> nodeToRootPath(TreeNode node, int data) {\r\n // write your code here\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 Treenode = new TreeNode(arr[IDX[0]++]);\r\n Treenode.left = createTree(arr, IDX);\r\n Treenode.right = createTree(arr, IDX);\r\n\r\n return Treenode;\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 int data = scn.nextInt();\r\n ArrayList<TreeNode> ans = nodeToRootPath(root, data);\r\n if (ans.size() == 0) System.out.println();\r\n for (TreeNode node : ans)\r\n System.out.print(node.val + \" \");\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"15\r\n4\r\n2\r\n1\r\n-1\r\n-1\r\n3\r\n-1\r\n-1\r\n6\r\n5\r\n-1\r\n-1\r\n7\r\n-1\r\n-1\r\n5","sampleOutput":"5 6 4 ","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":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"81bcdc8a-131d-47e5-aee8-f40c3cd0c730","name":"Node To Root Path Binary Tree","slug":"node-to-root-path-binary-tree","type":1}],"next":{"id":"91ebab58-3e1b-4817-a929-5a56aea6c5b2","name":"Node to Root Path Binary Tree","type":3,"slug":"node-to-root-path-binary-tree"},"prev":{"id":"7c199659-ac73-477e-87e4-5521b36c16b3","name":"Binary Search Tree Iterator 2","type":0,"slug":"binary-search-tree-iterator-2"}}}

# Node To Root Path Binary Tree

1. Given a Binary Tree, return root To Node path of it. 2. For more Information watch given video link below.

{"id":"9fd755c9-c1c6-47dc-aa76-c48b9c3c84a8","name":"Node To Root Path Binary Tree","description":"1. Given a Binary Tree, return root To Node path of it. \r\n2. For more Information watch given video link below.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Input 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\nvector<TreeNode*> nodeToRootPath(TreeNode* root, int data)\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 int data;\r\n cin >> data;\r\n vector<TreeNode*> ans = nodeToRootPath(root, data);\r\n if (ans.size() == 0)\r\n cout << endl;\r\n for (TreeNode* node : ans)\r\n {\r\n cout << node->val << \" \";\r\n }\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<TreeNode> nodeToRootPath(TreeNode node, int data) {\r\n // write your code here\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 Treenode = new TreeNode(arr[IDX[0]++]);\r\n Treenode.left = createTree(arr, IDX);\r\n Treenode.right = createTree(arr, IDX);\r\n\r\n return Treenode;\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 int data = scn.nextInt();\r\n ArrayList<TreeNode> ans = nodeToRootPath(root, data);\r\n if (ans.size() == 0) System.out.println();\r\n for (TreeNode node : ans)\r\n System.out.print(node.val + \" \");\r\n\r\n }\r\n\r\n public static void main(String[] args) {\r\n solve();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"15\r\n4\r\n2\r\n1\r\n-1\r\n-1\r\n3\r\n-1\r\n-1\r\n6\r\n5\r\n-1\r\n-1\r\n7\r\n-1\r\n-1\r\n5","sampleOutput":"5 6 4 ","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":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"81bcdc8a-131d-47e5-aee8-f40c3cd0c730","name":"Node To Root Path Binary Tree","slug":"node-to-root-path-binary-tree","type":1}],"next":{"id":"91ebab58-3e1b-4817-a929-5a56aea6c5b2","name":"Node to Root Path Binary Tree","type":3,"slug":"node-to-root-path-binary-tree"},"prev":{"id":"7c199659-ac73-477e-87e4-5521b36c16b3","name":"Binary Search Tree Iterator 2","type":0,"slug":"binary-search-tree-iterator-2"}}}

Editor

# Node To Root Path Binary Tree

medium

1. Given a Binary Tree, return root To Node path of it. 2. For more Information watch given video link below.

## Constraints

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

## Format

### Input

Input is managed for you.

### Output

Input is managed for you.

## Example

Sample Input

15 4 2 1 -1 -1 3 -1 -1 6 5 -1 -1 7 -1 -1 5

### Sample Output

5 6 4

Discussions

Show Discussion

Related Resources