zoukankan      html  css  js  c++  java
  • LeetCode 面试题04.03 特定深度节点链表

    题目描述链接:https://leetcode-cn.com/problems/list-of-depth-lcci/

    解题思路:采用广度优先搜索,进行层次遍历,每层访问时将每层的节点添加到相应层对应的链表当中,访问完一层将该层的链表放到答案数组中。

    具体LeetCode参考代码如下:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<ListNode*> listOfDepth(TreeNode* tree) {
            queue<TreeNode*>q;
            q.push(tree);
            vector<ListNode*>res;
            TreeNode *p;
            int len;
            while(!q.empty()){
                len=q.size();
                ListNode *root=new ListNode(0);
                ListNode *temp;
                temp=root;
                for(int i=0;i<len;++i){
                   p=q.front();
                   q.pop();
                   if(p->left){
                       q.push(p->left);
                   }
                   if(p->right){
                       q.push(p->right);
                   }
                   
                   ListNode *temp2=new ListNode(p->val);
                   
                   temp->next=temp2;
                  
                   temp=temp2;
                   
                }
               
                res.push_back(root->next);
            }
            return res;
        }
    };
  • 相关阅读:
    禅道安装
    logstash将配置写在多个文件
    原版Filebeat+ELK
    Filebeat+ELK部署文档
    A-2---Jenkins安装
    Linux ftp服务器搭建
    linux 网络命令
    yum安装时出现No more mirrors to try.
    kvm 修改虚拟机密码
    NFS安装
  • 原文地址:https://www.cnblogs.com/zzw-/p/13369012.html
Copyright © 2011-2022 走看看