假设二叉树中所有节点的值为int类型,采用二叉链存储。设计递归算法求二叉树中从根节点到叶子节点路径和最大的一条路径。
先序创建二叉树,输入0表示无左孩子或右孩子
#include<iostream>
using namespace std;
typedef struct BiTree
{
int data;
struct BiTree *left,*right;
}BiTree;
void CreatBiTree(BiTree *&T)
{
int temp;
cin>>temp;
if(temp!=0)
{
T=new BiTree;
T->data=temp;
CreatBiTree(T->left);
CreatBiTree(T->right);
}
else T=nullptr; //此行报错的话把nullptr改为NULL
}
int PathMax(BiTree *T)
{
if(T==0)return 0;
else
{
return max(T->data+PathMax(T->left),T->data+PathMax(T->right));
}
}
void Path(BiTree *T)
{
if(T)
{
cout<<T->data<<' ';
if(PathMax(T->left)>=PathMax(T->right))
{
Path(T->left);
}
else
{
Path(T->right);
}
}
}
int main()
{
BiTree *T;
CreatBiTree(T);
cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"最大和:"<<PathMax(T)<<endl<<"此路径为:";
Path(T);
return 0;
}