二叉树的基本操作
求二叉树中叶子结点数目的递归算法。
编写求二叉树深度的递归算法。
编写判断二叉树是否相似的递归算法
编写求二叉树左右子树互换的递归算法
1 /*求二叉树的叶子节点数*/ 2 Status leaf_num(BiTree T) 3 { 4 if (T == NULL) 5 return 0; 6 if (T->lchild == NULL && T->rchild == NULL) 7 return 1; 8 9 return (leaf_num(T->lchild) + leaf_num(T->rchild)); 10 } 11 12 /*求二叉树深度的递归算法*/ 13 Status height(Bitree T) 14 { 15 int u = 0, v = 0; 16 17 if (T==NULL) return 0; 18 u=height(T->lchild); 19 v=height(T->rchild); 20 if (u>v) return (u+1); 21 return (v+1); 22 } 23 24 25 /*判断二叉树是否相似的算法*/ 26 bool like(Tree B1,Tree B2) 27 { 28 if (B1 == NULL && B2 == NULL) 29 { 30 return true; 31 } 32 else if (B1 == NULL || B2 == NULL) 33 { 34 return false; 35 } 36 else 37 { 38 return like(B1->left,B2->left) && like(B1->right,B2->right); 39 } 40 41 } 42 43 /*编写求二叉树左右子树互换的算法的递归算法*/ 44 void swap(BiNode T){ 45 char temp; 46 47 if(T == NULL) return; 48 else{ 49 temp = root1->lchild; 50 root1->lchild = root1->rchild; 51 root1->rchild = temp; 52 swap(root1->lchild); 53 swap(root1->rchild); 54 } 55 }
浙公网安备 33010602011771号