標籤:style blog color os io ar div log sp
#include <iostream>using namespace std;struct TNode{ TNode * LeftChild; TNode * RightChild; char data;};TNode *CreateTree(){ TNode *pRoot=NULL; char data=0; cin>>data; if (data==‘#‘) { pRoot=NULL; } else { pRoot=(TNode*)malloc(sizeof(TNode)); pRoot->data=data; pRoot->LeftChild=CreateTree(); pRoot->RightChild=CreateTree(); } return pRoot;}int isTreeEqual(TNode *root1,TNode *root2){ bool isTree1NULL=(root1==NULL); bool isTree2NULL=(root2==NULL); if (isTree1NULL!=isTree2NULL)//兩節點不等 { return 0; } if (isTree2NULL&&isTree1NULL)//兩顆樹都為空白 相等 { return 1; } if (root1->data!=root2->data) { return 0; } return ( isTreeEqual(root1->LeftChild,root2->LeftChild)& isTreeEqual(root1->RightChild,root2->RightChild) ) | ( isTreeEqual(root1->LeftChild,root2->RightChild)& isTreeEqual(root1->RightChild,root2->LeftChild) );}void main(){ TNode *root1=CreateTree(); TNode* root2=CreateTree(); cout<<isTreeEqual(root1,root2);}
判斷兩棵二叉樹是否相等