Files
data-structures-and-algorithms/homework4/test1.cpp
T
2024-11-21 23:44:43 +08:00

30 lines
691 B
C++

/*
* 二叉树采用二叉链存储结构存放,结点值为int 类型,设计一个递归算法求二叉树 bt 中所有叶子结点值之和
*/
#include<iostream>
#include"BiTree.h"
using namespace std;
BiTree* createTreeFromArray(const int* values, int size)
{
BiTree* root = nullptr;
for (int i = 0; i < size; ++i)
{
BiTree::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* root = createTreeFromArray(testValues, size);
cout << "树结构为" << endl << root->toString() << endl;
cout << "叶子节点数据总和: " << BiTree::sum_leaf(root) << endl;
return 0;
}