void copy(BiTree T,BiTree &NewT){ //复制树 if(T == NULL){ NewT = NULL; return; }else { NewT = new BiTNode; NewT->data = T->data; copy(T->lchild,NewT->lchild); copy(T->rchild,NewT->rchild); } } int Depth(BiTree T){ //计算深度 if(T == NULL) return 0; else { int m = Depth(T->lchild); int n = Depth(T->rchild); if(m>n) return m+1; else return n+1; } } int NodeCount(BiTree T){ //统计节点数 if(T == NULL) return 0; else return NodeCount(T->lchild)+NodeCount(T->rchild)+1; } static int leaveCount = 0; int LeaveNodeCount(BiTree T){ //统计叶子节点数 if(T == NULL ) return 0; else{ if(T->lchild == NULL&&T->rchild == NULL) return leaveCount++; LeaveNodeCount(T->lchild); LeaveNodeCount(T->rchild); } } int LeaveNodeCount1(BiTree T){ //统计叶子节点数 if(T == NULL ) return 0; else{ if(T->lchild == NULL && T->rchild == NULL) return 1; else { return LeaveNodeCount(T->lchild)+LeaveNodeCount(T->rchild); } } } int compareTree(BiTree A1,BiTree A2){ //1表示相同,0表示不同 比较两个树 if(A1 == NULL && A2 == NULL){ return 1; } if((A1 == NULL) != (A2 == NULL)){ return 0; } if(A1->data == A2->data){ return (compareTree(A1->lchild,A2->lchild) &compareTree(A1->rchild,A2->rchild)); }else { return 0; } } void changeLR(BiTree &T){ //交换左右子树 if(T->lchild == NULL && T->rchild){ return ; }else { BiTree e = T; T->lchild = T->rchild; T->rchild = e->lchild; } changeLR(T->lchild); changeLR(T->rchild); } void DoubleTraverse(BiTree T){ //p148.(4) 双序遍历 if(T == NULL) return ; else { if(T->lchild == NULL&&T->rchild == NULL) //叶子节点只需要输出一次 cout << T->data; else { cout << T->data; //先序 + 中序(如果不把叶子结点拿出来,会输出两遍) DoubleTraverse(T->lchild); cout << T->data; DoubleTraverse(T->rchild); } } }
二叉树——复制、计算深度、统计节点数、统计叶子节点数、比较两个树、交换左右子树节点、双序遍历
原文:https://www.cnblogs.com/LuMinghao/p/14033089.html