Convert tree root->adjacency list

Can someone share the code to convert a tree root to an adjacency list

vector< vector < int > > con(TreeNode* root){

}

I know the logic, just wanted someone who already has the code to share!

You want a vector<vector<int>> ? Do we just arbitrarily give the numbers to the node? And do you want the adjacency list as a graph or a tree? As in, do you want the parent of a node included in it’s adjacency list, or just the children?

Tree is also a graph right?

Yeah parent too

Yes, tree is a graph. (An undirected acyclic graph). But you can represent it in an adjacency list just like you would represent a graph, or you can just have a vector for the children of each node (like an N-ary tree).

Well, if you want the adjacency list, this is how id write it:

Code
  1 int node = 1;
  2 unordered_map<TreeNode*, int> id;
  3 queue<TreeNode*> root;
  4 q.push(root);
  5 while(not q.empty())
  6 {
  7     TreeNode* cur = q.front(); q.pop();
  8     id[cur] = node++;
  9     if(cur->left)
 10         q.push(cur->left);
 11     if(cur->right)
 12         q.push(cur->right);
 13 }
 14 vector<int> adj[node];
 15 q.push(root);
 16 while(not q.empty())
 17 {
 18     TreeNode* cur = q.front(); q.pop();
 19     if(cur->left)
 20     {
 21         adj[id[cur]].push_back(id[cur->left]);
 22         adj[id[cur->left]].push_back(id[cur]);
 23         q.push(cur->left);
 24     }
 25     if(cur->right)
 26     {
 27         adj[id[cur]].push_back(id[cur->left]);
 28         adj[id[cur->left]].push_back(id[cur]);
 29         q.push(cur->left);
 30     }
 31 }

Is this any particular problem from leetcode?