leetcode-101. 对称二叉树
题目:
代码:
#include <iostream>
using namespace std;
typedef struct TreeNode{
int val;
struct TreeNode *left,*right;
}TreeNode,*BiTree;
void create(BiTree &T){
char ch;
cin>>ch;
if(ch=='#'){
T=NULL;
return;
}
T=new TreeNode;
T->val=ch-'0';
create(T->left);
create(T->right);
}
bool check(TreeNode *p,TreeNode *q){
if(!p && !q){
return true;
}else if(!p || !q){
return false;
}
return p->val==q->val && check(p->left,q->right) && check(p->right,q->left);
}
bool isSymmetric(TreeNode* root) {
return check(root,root);
}
int main(){
bool res;
TreeNode* root;
create(root);
res=isSymmetric(root);
if(res){
cout<<"true";
}else{
cout<<"false";
}
return 0;
}