void def(struct Node* root,vector<int>&ans,int k); vector<int> Kdistance(struct Node *root, int k) { vector<int>ans; def(root,ans,k); return ans; } void def(struct Node* root,vector<int>&ans,int k) { if(!root) return; if(k==0) ans.push_back(root->data); def(root->left,ans,k-1); def(root->right,ans,k-1); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter