zoukankan      html  css  js  c++  java
  • careercup-C和C++ 13.7

    13.7 写一个函数,其中一个参数是指向Node结构的指针,返回传入数据结构的一份完全拷贝。 Node结构包含两个指针,指向另外两个Node。

    C++实现代码:

    typedef map<Node*, Node*> NodeMap;
    Node* copy_recursive(Node *cur, NodeMap &nodeMap){
        if(cur == NULL){
            return NULL;
        }
        NodeMap::iterator i = nodeMap.find(cur);
        if (i != nodeMap.end()){
            // we’ve been here before, return the copy
            return i->second;
        }
        Node *node = new Node;
        nodeMap[cur] = node; // map current node before traversing links
        node->ptr1 = copy_recursive(cur->ptr1, nodeMap);
        node->ptr2 = copy_recursive(cur->ptr2, nodeMap);
        return node;
    }
    Node* copy_structure(Node* root){
        NodeMap nodeMap; // we will need an empty map
        return copy_recursive(root, nodeMap);
    }
  • 相关阅读:
    python爬虫
    RMQ算法
    组合数
    水池数目
    jQuery 拼接事件
    ORACLE
    day 75
    day74 vue框架
    day73 vue框架
    day 72 vue框架
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4157240.html
Copyright © 2011-2022 走看看