• Aspen丶Sun


    #include <iostream>
    #include <vector>
    using namespace std;
    #ifndef DBM_MTREE_H
    #define DBM_MTREE_H
    typedef int T;
    typedef struct MNode {
        T element;
        vector<MNode*> children;
        MNode *Parent;
    } MNode;
    
    class MTree {
    private:
        MNode *root;
    public:
        void init(MNode *root);
        void putChild(MNode* node,MNode* parent);
        void putChildren(vector<MNode*> nodes, MNode *parent);
        void tranversal(MNode *root);
        void tranversal();
        int getMaxDepth(MNode *root,vector<MNode*> nodes);
    };
    
    void MTree::init(MNode *root) { this->root = root; }
    
    void MTree::putChild(MNode *node, MNode *parent) {
        parent->children.push_back(node);
        node->Parent = parent;
    }
    
    void MTree::putChildren(vector<MNode *> nodes, MNode *parent) {
        for (int i = 0; i < nodes.size(); ++i) {
            putChild(nodes[i], parent);
        }
    }
    
    void MTree::tranversal() {
        this->tranversal(this->root);
    }
    
    void MTree::tranversal(MNode *root) {
        vector<MNode *> nodes = root->children;
        for (int i = 0; i < nodes.size(); ++i) {
            if (nodes[i]->children.size() > 0)
                tranversal(nodes[i]);
            else
                cout << nodes[i]->element << ",";
        }
        cout << root->element << ",";
    }
    
    int MTree::getMaxDepth(MNode *root,vector<MNode*> nodes) {
        auto iResult = 0;
    
        return iResult;
    }
    
    class Solution {
    public:
        vector<vector<int>> levelOrder(Node* root) {
            queue<Node*> que;
            if (root != NULL) que.push(root);
            vector<vector<int>> result;
            while (!que.empty()) {
                int size = que.size();
                vector<int> vec;
                // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
                for (int i = 0; i < size; i++) {
                    Node* node = que.front();
                    que.pop();
                    vec.push_back(node->val);
                    for(auto it : node -> children){
                        if(it) que.push(it); 
                    }
                    //if (node->left) que.push(node->left);
                    //if (node->right) que.push(node->right);
                }
                result.push_back(vec);
            }
            return result;
        }
    };
    
    
  • 相关阅读:
    Linux
    前端
    第一章 初识 MyBatis
    mysql 复习
    五 、 Kafka producer 拦截器(interceptor) 和 六 、Kafka Streaming案例
    spark graphx图计算
    四、Kafka API 实战
    三、Kafka工作流程分析
    二、Kafka集群部署
    一、KafKa概述
  • 原文地址:https://www.cnblogs.com/shenxiaodou/p/16307606.html
Copyright © 2020-2023  润新知