#include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left; Node *right; }; struct TreeList { Node *root; Node *generate_tree(vector<int> data, size_t start, size_t end) { if (start >= end) { return new Node{data[start], nullptr, nullptr}; } size_t mid = (start + end) / 2; Node *node = new Node(); node->data = data[mid]; node->left = generate_tree(data, start, mid - 1); node->right = generate_tree(data, mid + 1, end); return node; } TreeList() { root = nullptr; } TreeList(vector<int> data) { root = generate_tree(data, 0, data.size() - 1); } void delete_tree(Node *node) { if (node == nullptr) { return; } delete_tree(node->left); delete_tree(node->right); delete node; } ~TreeList() { delete_tree(root); } void pre_print_tree(Node *node, size_t level) { if (node == nullptr) { return; } cout << node->data << " " << level << endl; pre_print_tree(node->left, level + 1); pre_print_tree(node->right, level + 1); } }; int main() { TreeList tree{{1, 2, 3, 4, 5, 6, 7}}; tree.pre_print_tree(tree.root, 0); return 0; }