题解 | #对称的二叉树#
对称的二叉树
http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
bool dfs(TreeNode* left, TreeNode* right) {
if(!left) return !right;
if(!right) return !left;
if(left->val != right->val) return false;
return dfs(left->right, right->left) && dfs(left->left, right->right);
}
bool isSymmetrical(TreeNode* pRoot) {
if(!pRoot)
return true;
return dfs(pRoot->left, pRoot->right);
}
};