[C++] Nice Idea | Takes less 📦 than 92%
Approach\nThe following nodes will be part of the answer:\n-> Children Nodes of target at k distance\n-> Nodes that share the same parent(previous generation) a
# Approach\nThe following nodes will be part of the answer:\n-> Children Nodes of `target` at `k` distance\n-> Nodes that share the same `parent`(previous generation) as target and the sum of both distances if = `k` \n\n\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n    vector<int> v={};\n    int helper(TreeNode* root, TreeNode* target, int k, map<TreeNode*,pair<int,char>>&m){\n        if(!root)return INT_MAX-400;\n        if(root==target)return 0;\n        int l = 1+helper(root->left,target,k,m);\n        int r = 1+helper(root->right,target,k,m);\n        if(l<=k){\n            m[root]={l,\'r\'};\n        }\n        if(r<=k){\n            m[root]={r,\'l\'};\n        }\n        return min(l,r);\n    }\n    void dis(TreeNode* root, char dir, int d){\n        if(d==0){\n            if(root)v.push_back(root->val);\n            return;\n        }\n        if(!root)return;\n        if(dir==\'l\'||dir==\'b\'){\n            dis(root->left,\'b\',d-1);\n        }\n        if(dir==\'r\'||dir==\'b\'){\n            dis(root->right,\'b\',d-1);\n        }\n    }\n    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {\n        map<TreeNode*,pair<int,char>> m;\n        helper(root,target,k,m); //first type of nodes\n        dis(target,\'b\',k); //setup for second type of nodes\n        for(auto&i:m){\n            //calculating second type of nodes\n            dis(i.first,i.second.second,k-i.second.first);\n        }\n        return v;\n    }\n};\n```\n\n```\n\n  int   int   if if if  set       set   OOO    for for for EEEEE  \n  int   int   if    if   set     set  O     O      for     E      \n  int   int   if if if    set   set   O     O      for     EEE    \n  int   int   if           set set    O     O      for     E      \n   int int    if             set        OOO        for     EEEEE  \n```
['C++']