cay 2

//Kiem tra  nut la

char IsLeaf(Node *p){

   return (p->Left == NULL) && (p->Right == NULL);

}

//Dem nut la

int CountLeaf(Tree T) {

   if(T == NULL)

      return 0;

   else

      if(IsLeaf(T))

         return 1;

      else

         return CountLeaf(T->Left) + CountLeaf(T->Right);

}

//Dem so nut

int CountNode(Tree T) {

   if( T == NULL)

      return 0;

   else

      return 1 + CountNode(T->Left) + CountNode(T->Right);

}

//Dem sp node la chan,le

int demle (Node &root)

{

   if(!root)   return 0;

   int kq= demle(root->left) + demle(root->right);

   if(root->key%2==1 && !root->left && !root->right)

return 1+kq;

   return kq;

}

//Liet ke cac node la tang dan

void Lietketang (Node &root)

{

   if(!root)   return;

   Lietketang (root->left);

   if(!root->left && !root->right)

cout<<root->key<<"";

   Lietketang(root->right);

}

//Tinh tong cac khoa trong cay

void Tinhtong(Node &root)

{

   if(!root)  return 0;

   int a = Tinhtong(root-> left);

   int b = Tinhtong(root-> right);

   int c = root->key;

   return a + b + c;

}

//Dem so khoa o muc 2 cua cay

int khoamuc2 (Node &root,int muc=0)

{

   if(!root || muc>2)

        return 0;

   int d = khoamuc2(root->left ,muc+1)

   if(muc==2)

d++;

   d+=khoamuc2(root->right,muc+1);

   return 1;

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: