Files
data-structures-and-algorithms/homework4/test1.cpp
T
2024-11-22 18:09:17 +08:00

43 lines
932 B
C++

/*
* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
*/
#include <iostream>
#include "BiTree.h"
using namespace std;
BiTree<int>* createTreeFromArray(const int* values, int size)
{
BiTree<int>* root = nullptr;
for (int i = 0; i < size; ++i)
{
BiTree<int>::insertNode(root, values[i]);
}
return root;
}
int main()
{
int testValues[] = {7, 3, 10, 1, 5, 9, 12};
int size = sizeof(testValues) / sizeof(testValues[0]);
BiTree<int>* root = createTreeFromArray(testValues, size);
if (root)
{
cout << "树结构为:" << endl;
cout << root->toString() << endl;
cout << "叶子节点数据总和: " << BiTree<int>::sum_leaf(root) << endl;
root->destroy();
delete root;
}
else
{
cout << "树为空。" << endl;
}
return 0;
}