深搜
详见代码: 对称比对!
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
private:
bool helper(TreeNode* pLeft, TreeNode* pRight){
if(pLeft == NULL && pRight == NULL) return true;
else if(pLeft == NULL || pRight == NULL) return false;
return pLeft -> val == pRight -> val
&& helper(pLeft -> left, pRight -> right)
&& helper(pLeft -> right, pRight -> left);
}
public:
bool isSymmetrical(TreeNode* pRoot) {
if(pRoot == NULL) return true;
return helper(pRoot -> left, pRoot -> right);
}
};