对于本题,想到一个中序遍历后,判别是否为回文串的方法,却WA多次
class Solution {
public:
vector<int> vectorValue;
void inOrder(TreeNode* root)
{
if(root!=NULL)
{
inOrder(root->left);
vectorValue.push_back(root->val);
inOrder(root->right);
}
}
bool isSymmetric(TreeNode *root) {
if(root==NULL)return true;
vectorValue.clear();
inOrder(root);
int length=vectorValue.size();
for(int i=0;i<length/2;i++)
{
if(vectorValue[i]!=vectorValue[length-1-i])
return false;
}
return true;
}
};
一种笨方法解决了这个问题,那便是逐层检查逐层扩展,检查每一层的节点是否互相对称。
附自己调试代码 主要参与了http://blog.csdn.net/greenapple_shan/article/details/25976165
/*
* LeetCode--Symmetric Tree
* date 2014/5/16
* state AC
*/
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
/**
* Definition for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
/*
vector<int> vectorValue;
void inOrder(TreeNode* root)
{
if(root!=NULL)
{
inOrder(root->left);
vectorValue.push_back(root->val);
inOrder(root->right);
}
}
bool isSymmetric(TreeNode *root) {
if(root==NULL)return true;
vectorValue.clear();
inOrder(root);
int length=vectorValue.size();
for(int i=0;i<length/2;i++)
{
if(vectorValue[i]!=vectorValue[length-1-i])
return false;
}
return true;
}
bool isSymRec(TreeNode* left,TreeNode* right)
{
//递归结束条件
if(left==NULL && right==NULL)
return true;
else if(left==NULL || right==NULL)
return false;
//递归体
return(left->val == right->val //结点数值比较
&& isSymRec(left->left,right->right) //左子树左对右子树右
&& isSymRec(left->right,right->left));//左子树右对右子树左
}
bool isSymmetric(TreeNode* root)
{
if(root==NULL)return true;
else isSymRec(root->left,root->right);
}
*/
bool isSymmetric(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==nullptr) return true;
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(root->left);
q2.push(root->right);
while( !q1.empty() || !q2.empty()) {
TreeNode* t1 = q1.front();
TreeNode* t2 = q2.front();
q1.pop();
q2.pop();
if(t1==nullptr && t2==nullptr) continue;
if(t1==nullptr || t2==nullptr) return false;
if(t1->val!=t2->val) return false;
q1.push(t1->left);
q1.push(t1->right);
q2.push(t2->right);
q2.push(t2->left);
}
return true;
}
};
TreeNode* create()
{
char ch;
cin>>ch;
TreeNode* root;
if(ch==‘#‘)
return NULL;
else
{
root=new TreeNode(ch);
root->left=create();
root->right=create();
return root;
}
}
bool isSame(vector<int>& vectorValue)
{
int length=vectorValue.size();
for(int i=0;i<length;i++)
{
if(vectorValue[i]!=vectorValue[length-1-i])
return false;
}
}
int main()
{
//cout << "Hello world!" << endl;
TreeNode* t;
t=create();
Solution Solu;
Solu.inOrder(t);
int len=Solu.vectorValue.size();
for(int i=0;i<len;i++)
//cout<<Solu.vectorValue[i]<<" ";
printf("%c ",Solu.vectorValue[i]);
cout<<endl;
if(Solu.isSymmetric(t))
cout<<"true"<<endl;
else cout<<"false"<<endl;
/*
vector<int> vec;
int n;
cin>>n;
int t;
for(int i=0;i<n;i++)
{
cin>>t;
vec.push_back(t);
}
if(isSame(vec))cout<<"true"<<endl;
else cout<<"false"<<endl;
*/
return 0;
}
LeetCode--Symmetric Tree,布布扣,bubuko.com
原文:http://blog.csdn.net/greenapple_shan/article/details/25975531