C++ 无限树递归

C++ 无限树递归,c++,recursion,data-structures,tree,C++,Recursion,Data Structures,Tree,我已经做了一个代码来从一个向量构造一个二叉树。问题是,当我调用inoder(root)时,它将进入无限递归,即root永远不会变成nullptr #include <iostream> #include <vector> using namespace std; class Node { public: int data; Node* left; Node* right; explicit Node(int element) {

我已经做了一个代码来从一个向量构造一个二叉树。问题是,当我调用
inoder(root)
时,它将进入无限递归,即root永远不会变成
nullptr

#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    explicit Node(int element) {
        data = element;
        left = nullptr;
        right= nullptr;
    }
};

Node* construct_tree(Node* root, vector<int> &vec, int i) {
    if(i < vec.size()) {
        Node* new_node = new Node(vec[i]);
        root = new_node;
        root->left = construct_tree(root, vec, 2*i + 1);
        root->right = construct_tree(root, vec, 2*i + 2);
    }
    return root;
}

void inOrder(Node* root) {
    if (root != nullptr) {
        inOrder(root->left);
        int n = root->data;
        cout << n << " ";
        inOrder(root->right);
    }
}

int main() {
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    Node *root = nullptr;
    root = construct_tree(root, vec, 0);
    cout << root->data << " ";
    cout << root->left->data;
    inOrder(root);
    return 0;
}
#包括
#包括
使用名称空间std;
类节点{
公众:
int数据;
节点*左;
节点*右;
显式节点(int元素){
数据=元素;
左=空PTR;
右=空PTR;
}
};
节点*构造树(节点*根、向量和向量、int i){
如果(ileft=构造_树(root,vec,2*i+1);
root->right=构造_树(root,vec,2*i+2);
}
返回根;
}
无效索引(节点*根){
if(root!=nullptr){
顺序(根->左);
int n=根->数据;

cout您正在从
construct_tree
返回
root
,因此当条件
i
为false时,它可以将
root
分配给
root->left
root->right
,创建圆

在这种情况下,您不需要参数
root
,因为它的值从未在函数
construct\u tree
中使用过

试试这个:

#include <iostream>
#include <vector>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    explicit Node(int element) {
        data = element;
        left = nullptr;
        right= nullptr;
    }
};

Node* construct_tree( vector<int> &vec, int i) {
    if(i < vec.size()) {
        Node* new_node = new Node(vec[i]);
        // stop using root and use new_node directly
        new_node->left = construct_tree(vec, 2*i + 1);
        new_node->right = construct_tree(vec, 2*i + 2);
        return new_node;
    } else {
        // return nullptr when the condition is false
        return nullptr;
    }
}

void inOrder(Node* root) {
    if (root != nullptr) {
        inOrder(root->left);
        int n = root->data;
        cout << n << " ";
        inOrder(root->right);
    }
}

int main() {
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    Node *root = nullptr;
    root = construct_tree(vec, 0); // remove the argument root
    cout << root->data << " ";
    cout << root->left->data;
    inOrder(root);
    return 0;
}
#包括
#包括
使用名称空间std;
类节点{
公众:
int数据;
节点*左;
节点*右;
显式节点(int元素){
数据=元素;
左=空PTR;
右=空PTR;
}
};
节点*构造树(向量和向量,int i){
如果(i左=构造_树(vec,2*i+1);
新建_节点->右=构造_树(vec,2*i+2);
返回新的_节点;
}否则{
//当条件为false时返回null ptr
返回空ptr;
}
}
无效索引(节点*根){
if(root!=nullptr){
顺序(根->左);
int n=根->数据;

cout
Node*insertLevelOrder(intarr[],Node*root,inti,intn){if(ileft=insertLevelOrder(arr,root->left,2*i+1,n);root->right=insertLevelOrder(arr,root->right,2*i+2,n);}返回root;}
该函数如何处理数组而不是向量?正如@MikeCAT在解决方案中所示,错误出现在
构造树
例程中。您在设计函数时出错。它递归地构建一棵树,并显式地将子树分配给当前节点的
成员。a因此,它应该返回生成的树的根,这是一个
新节点
(如果
i
太大,可能是
nullptr
),但不是由参数传递的
根。