A list of single layer nodes of binary tree

Title Description
For a binary tree, design an algorithm to create a linked list containing all nodes at a certain depth.
Given the root node pointer TreeNode* root of a binary tree and the depth of nodes on the list, please return a list ListNode, which represents the values of all nodes on the depth. Please link from left to right on the tree to ensure that the depth does not exceed the height of the tree. The value of nodes on the tree is a non negative integer and does not exceed 100000.

struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {

struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
class TreeLevel {
    ListNode* getTreeLevel(TreeNode* root, int dep) {
        ListNode* res = NULL;
            return res;
         queue <TreeNode*> q;

        vector<int> temp;
        int num = 1;
         while (!q.empty()) 
            vector<int> te;
            int size=q.size();
            for (int i=0;i<size;i++) 
            {      //Process data one layer at a time
                TreeNode *node=q.front();
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);

             if(num == dep)
                 temp = te;
       ListNode* r =  new ListNode(0); 
        ListNode* pc = res;

        for(int i = 0;i < temp.size();++i)
            ListNode* pm = new ListNode(temp[i]);
            pc->next = pm;
            pc = pc->next;
        return r->next;

Posted on Thu, 30 Apr 2020 17:51:29 -0700 by Hellbringer2572