C++ 我有两棵二叉树。我想在不改变输入树的情况下深度复制两个二叉树的结果 类节点{ 公众: 友元类二进制树头; 节点(int值,节点*左,节点*右) { 这个->值=值; 这个->左=左; 这个->右=右; } int getValue()常量 { 返回值; } 节点*getLC()常量 { 左转; } 节点*getRC()常量 { 返还权; } 无效设置值(int值){ 这个->值=值; } void setLC(节点*左侧){ 这个->左=左; } void setRC(节点*右侧){ 这个->右=右; } 公众: int值; 节点*左; 节点*右; }; 类binarytreead{ 公众: 静态节点*cpNode(常量节点*源) { if(source==nullptr) { 返回空ptr; } 返回源==nullptr?nullptr :新建节点(源->值, cpNode(源->左), cpNode(源->右); } 静态节点*添加(节点*t1,节点*t2){ 如果(t1==NULL){ 返回t2; } 如果(t2==NULL){ 返回t1; } t1->value+=t2->value; t1->left=添加(t1->left,t2->left); t1->right=添加(t1->right,t2->right); 返回t1; } 无效显示(节点*节点){ while(节点!=NULL){ cout getValue()getLC()); 显示(节点->getRC()); 返回; } } }; int main(){ 二元树型bt; 节点root1(3,NULL,NULL); 节点root2(1,&root1,NULL); 节点root3(3,NULL,NULL); 节点root4(5,&root2,&root3); 节点root5(5,NULL,NULL); 节点root6(6,NULL和root5); 节点root7(5,NULL,NULL); 节点root8(2,&root6,&root7); Node*root9=binarytreead::add(&root4,&root8); Node*root10=binarytreeeadd::cpNode(root9); bt.display(root10); 返回0; } 我合并了两棵树t1和t2,并将结果存储回t1 (添加函数)。 函数调用:Node*root9=binarytreeeadd::add(&root4,&root8) 然后我将t1深度复制到源代码(cpNode函数)。 函数调用:Node*root10=binarytreeeadd::cpNode(root9) 我使用了显示功能来打印深度复制的结果。 函数调用:bt.display(root10)

C++ 我有两棵二叉树。我想在不改变输入树的情况下深度复制两个二叉树的结果 类节点{ 公众: 友元类二进制树头; 节点(int值,节点*左,节点*右) { 这个->值=值; 这个->左=左; 这个->右=右; } int getValue()常量 { 返回值; } 节点*getLC()常量 { 左转; } 节点*getRC()常量 { 返还权; } 无效设置值(int值){ 这个->值=值; } void setLC(节点*左侧){ 这个->左=左; } void setRC(节点*右侧){ 这个->右=右; } 公众: int值; 节点*左; 节点*右; }; 类binarytreead{ 公众: 静态节点*cpNode(常量节点*源) { if(source==nullptr) { 返回空ptr; } 返回源==nullptr?nullptr :新建节点(源->值, cpNode(源->左), cpNode(源->右); } 静态节点*添加(节点*t1,节点*t2){ 如果(t1==NULL){ 返回t2; } 如果(t2==NULL){ 返回t1; } t1->value+=t2->value; t1->left=添加(t1->left,t2->left); t1->right=添加(t1->right,t2->right); 返回t1; } 无效显示(节点*节点){ while(节点!=NULL){ cout getValue()getLC()); 显示(节点->getRC()); 返回; } } }; int main(){ 二元树型bt; 节点root1(3,NULL,NULL); 节点root2(1,&root1,NULL); 节点root3(3,NULL,NULL); 节点root4(5,&root2,&root3); 节点root5(5,NULL,NULL); 节点root6(6,NULL和root5); 节点root7(5,NULL,NULL); 节点root8(2,&root6,&root7); Node*root9=binarytreead::add(&root4,&root8); Node*root10=binarytreeeadd::cpNode(root9); bt.display(root10); 返回0; } 我合并了两棵树t1和t2,并将结果存储回t1 (添加函数)。 函数调用:Node*root9=binarytreeeadd::add(&root4,&root8) 然后我将t1深度复制到源代码(cpNode函数)。 函数调用:Node*root10=binarytreeeadd::cpNode(root9) 我使用了显示功能来打印深度复制的结果。 函数调用:bt.display(root10),c++,data-structures,stl,C++,Data Structures,Stl,我的问题是: 如何使用cpNode函数将t1和t2的内容直接复制到源节点 合并树后,不应更改t1和t2的内容 这是您需要的函数。它是递归的,就像输入函数一样。我使用奇数选项简化了逻辑,允许递归,即使一个节点为空。这使事情变得更容易,因为否则,在这种情况下,我需要单独的代码来复制输入节点 class Node{ public: friend class BinaryTreeAdd; Node(int value, Node* left, Node* right) { this->

我的问题是:

  • 如何使用cpNode函数将t1和t2的内容直接复制到源节点

  • 合并树后,不应更改t1和t2的内容


  • 这是您需要的函数。它是递归的,就像输入函数一样。我使用奇数选项简化了逻辑,允许递归,即使一个节点为空。这使事情变得更容易,因为否则,在这种情况下,我需要单独的代码来复制输入节点

    class Node{
    public:
    friend class BinaryTreeAdd;
    Node(int value, Node* left, Node* right)
    {
        this->value = value;
        this->left = left;
        this->right = right;
    }
     int getValue() const
    {
        return value;
    }
    
    Node* getLC() const
    {
        return left;
    }
    
    Node* getRC() const
    {
        return right;
    }
    void setValue(int value) {
        this->value = value;
    }
    void setLC(Node* left) {
        this->left = left;
    }
    void setRC(Node* right) {
        this->right = right;
    }
     public:
       int value;
       Node* left;
       Node* right;
     };
    class class BinaryTreeAdd {
     public:
    static Node* cpNode(const Node* source)
    {
        if (source == nullptr)
        {
            return nullptr;
        }
        return source == nullptr? nullptr
               : new Node(source->value,
                cpNode(source->left),
                cpNode(source->right));
    }
     static Node* add(Node *t1, Node *t2){
        if (t1 == NULL) {
             return t2;
        }
        if (t2 == NULL) {
             return t1;
        } 
        t1->value += t2->value;
        t1->left=add(t1->left, t2->left);
        t1->right=add(t1->right, t2->right);
        return t1;
    }
    void display(Node * node){
        while (node != NULL) {
            cout << node->getValue() << endl;
                display(node->getLC());
                display(node->getRC());
            return;
        }
    }
    };
    int main(){
    BinaryTreeAdd bt;
    Node root1(3, NULL, NULL);
    Node root2(1, &root1, NULL);
    Node root3(3, NULL, NULL);
    Node root4(5, &root2, &root3);
    
    Node root5(5, NULL, NULL);
    Node root6(6, NULL, &root5);
    Node root7(5, NULL, NULL);
    Node root8(2, &root6, &root7);
    
    Node *root9 = BinaryTreeAdd::add(&root4, &root8);
    Node *root10 = BinaryTreeAdd::cpNode(root9);
    bt.display(root10);
    return 0;
    }
    

    这是一种非常奇怪的添加二叉树的方法。你确定你在做有意义的事情吗?是的。我肯定。我在面试中有这个问题。我没能解决它。所以我需要帮助来解决这个问题。
    // This function returns a newly-created
    // node that merges the supplied input nodes
    // per the requested algorithm.
    // Either t1 or t2 or both can be NULL
    static Node* add(Node *t1, Node *t2)
    {
    
        // We don't want to create a new node if
        // both input nodes are NULL
        if (t1 == NULL) && (t2 == NULL)
            return NULL;
    
        return new Node (
              // The value is the sum of the input node
              // values or the input node value if just one
              (t1 ? t1->value : 0) + (t2 ? t2->value : 0),
    
              // The left node of the new node is the sum
              // of the input left nodes
              add (t1 ? t1->left : NULL, t2 ? t2->left : NULL),
    
              // The right node of the new node is the sum
              // of the input right nodes
              add (t1 ? t1->right : NULL, t2  ? t2->right : NULL));
    }