首页 > 编程知识 正文

计算二叉树节点数算法,二叉树遍历算法c语言

时间:2023-05-05 13:33:33 阅读:166781 作者:4923

#includeiostream

using namespace std;

运用递归思维求解二叉树遍历问题(个别考虑并写高难度的)//能很好地了解递归思想

typedef struct xx

{

int age;

xx* lchild;

xx* rchild;

}tree,* bitree;

(bitreet,bitree newt ) )。

{

if(t==null ) )。

{

返回;

}

else

{

newt=new xx;

newt-age=t-age;

复印(t-lchild,newt-lchild );

复制(t-rchild,newt-rchild );

}

将//t树复制到newt树//newt树=t树

intdepth(bitreet )。

{

int m,n;

if(t==null ) )。

{

返回0;

}

else {

m=Depth(t-Lchild );

n=Depth(t-Lchild );

return m n? m 1: n 1; //三目运算符

}

//求深()递归思想)哪里深

intnodnum(bitreet )。

{

if(t==null ) )。

{

返回0;

}

else

returnnodnum(t-lchild ) nod num (t-rchild ) 1; //1加上根节点

//求出节点的总数//按节点遍历(递归思想) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 652 )

输入长度(bitreet ) )。

{

if(t==null ) )。

{

返回0; //不存在直接向根节点返回0的内容

}

if (t-lchild==nullt-rchild==null ) () ) ) ) ) ) )。

{

返回1; //只有根节点返回1

}

else

{

返回长度(t-rchild )长度(t-rchild );

(//递归地按叶的每个节点求出)当一个节点左右都为NULL时返回1,依次递归地相加) ) )。

//求出叶的节点数(递归思想) ) )

int main () )

{

返回0;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。