`{"id":"9e0b3f2b-70f8-4518-9709-a47192c35a2d","name":"All Nodes Distance K In Binary Tree","description":"1. Given the root of a binary tree.\r\n2. The value of a target node target, and an integer k.\r\n3. You have return an array of the values of all nodes that have a distance k from the target node.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you. \r\n","constraints":"1. The number of nodes in the tree is in the range [1, 10000].\r\n2. -1000 &lt;= Node.val &lt;= 1000\r\n3. All the values Node.val are unique.\r\n4. target is the value of one of the nodes in the tree.\r\n5. 0 &lt;= k &lt;= 1000","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*> distanceK(TreeNode* root, int target, int k)\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 > arr.size() || arr[IDX] == -1)\r\n {\r\n IDX++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode* node = new TreeNode(arr[IDX++]);\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 target, k;\r\n cin >> target >> k;\r\n vector<TreeNode*> ans = distanceK(root, target, k);\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<Integer> distanceK(TreeNode root, int target, int k) {\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 > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n TreeNode Treenode = new TreeNode(arr[IDX++]);\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;\r\n TreeNode root = createTree(arr, IDX);\r\n int target = scn.nextInt();\r\n int k = scn.nextInt();\r\n\r\n ArrayList<Integer> ans = distanceK(root, target, k);\r\n if (ans.size() == 0)\r\n System.out.println();\r\n for (Integer ele : ans)\r\n System.out.println(ele + \" \");\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\n2 2","sampleOutput":"6 \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":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"aa787073-4076-420f-9d87-67de2305dbd8","name":"All Nodes Distance K In Binary Tree","slug":"all-nodes-distance-k-in-binary-tree","type":1}],"next":{"id":"45f35b19-d1c5-47e5-ba5e-0376a39c4559","name":"All Nodes Distance K In Binary Tree","type":3,"slug":"all-nodes-distance-k-in-binary-tree"},"prev":{"id":"23c6794c-b11b-49d5-b479-294843828b0c","name":"Count All Single Child Parent In Binary Tree","type":0,"slug":"count-all-single-child-parent-in-binary-tree"}}}`

# All Nodes Distance K In Binary Tree

1. Given the root of a binary tree. 2. The value of a target node target, and an integer k. 3. You have return an array of the values of all nodes that have a distance k from the target node.

`{"id":"9e0b3f2b-70f8-4518-9709-a47192c35a2d","name":"All Nodes Distance K In Binary Tree","description":"1. Given the root of a binary tree.\r\n2. The value of a target node target, and an integer k.\r\n3. You have return an array of the values of all nodes that have a distance k from the target node.\r\n","inputFormat":"Input is managed for you.\r\n","outputFormat":"Output is managed for you. \r\n","constraints":"1. The number of nodes in the tree is in the range [1, 10000].\r\n2. -1000 &lt;= Node.val &lt;= 1000\r\n3. All the values Node.val are unique.\r\n4. target is the value of one of the nodes in the tree.\r\n5. 0 &lt;= k &lt;= 1000","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*> distanceK(TreeNode* root, int target, int k)\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 > arr.size() || arr[IDX] == -1)\r\n {\r\n IDX++;\r\n return nullptr;\r\n }\r\n\r\n TreeNode* node = new TreeNode(arr[IDX++]);\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 target, k;\r\n cin >> target >> k;\r\n vector<TreeNode*> ans = distanceK(root, target, k);\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<Integer> distanceK(TreeNode root, int target, int k) {\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 > arr.length || arr[IDX] == -1) {\r\n IDX++;\r\n return null;\r\n }\r\n TreeNode Treenode = new TreeNode(arr[IDX++]);\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;\r\n TreeNode root = createTree(arr, IDX);\r\n int target = scn.nextInt();\r\n int k = scn.nextInt();\r\n\r\n ArrayList<Integer> ans = distanceK(root, target, k);\r\n if (ans.size() == 0)\r\n System.out.println();\r\n for (Integer ele : ans)\r\n System.out.println(ele + \" \");\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\n2 2","sampleOutput":"6 \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":"2e9df04c-be14-441c-9a18-8b5a8ca6596d","name":"Trees For Intermediate","slug":"trees-for-intermediate-9994","type":0},{"id":"aa787073-4076-420f-9d87-67de2305dbd8","name":"All Nodes Distance K In Binary Tree","slug":"all-nodes-distance-k-in-binary-tree","type":1}],"next":{"id":"45f35b19-d1c5-47e5-ba5e-0376a39c4559","name":"All Nodes Distance K In Binary Tree","type":3,"slug":"all-nodes-distance-k-in-binary-tree"},"prev":{"id":"23c6794c-b11b-49d5-b479-294843828b0c","name":"Count All Single Child Parent In Binary Tree","type":0,"slug":"count-all-single-child-parent-in-binary-tree"}}}` Editor

# All Nodes Distance K In Binary Tree

medium

1. Given the root of a binary tree. 2. The value of a target node target, and an integer k. 3. You have return an array of the values of all nodes that have a distance k from the target node.

## Constraints

1. The number of nodes in the tree is in the range [1, 10000]. 2. -1000 <= Node.val <= 1000 3. All the values Node.val are unique. 4. target is the value of one of the nodes in the tree. 5. 0 <= k <= 1000

## Format

### Input

Input is managed for you.

### Output

Output is managed for you.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}15 4 2 1 -1 -1 3 -1 -1 6 5 -1 -1 7 -1 -1 2 2```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}6 ```

Discussions

Show Discussion

Related Resources 