题目连接:Leetcode 101 Symmetric Tree
解题思路:递归判定,当前值需要相同,a的左子树与b的右子树对称,a的右子树与b的左子树对称。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool symmetric(TreeNode* first, TreeNode* second) {
if (first == NULL && second == NULL) return true;
if (first == NULL || second == NULL) return false;
if (first->val != second->val) return false;
return symmetric(first->left, second->right) && symmetric(first->right, second->left);
}
bool isSymmetric(TreeNode* root) {
return symmetric(root, root);
}
};