/**
* 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 isSymmetric(TreeNode* root) {
if(!root) return true;
return helper(root->left, root->right);
}
private:
bool helper(TreeNode* root1, TreeNode* root2) {
if(!root1 && !root2) return true;
if(!root1 || !root2) return false;
if(root1->val != root2->val) return false;
return helper(root1->left, root2->right) && helper(root1->right, root2->left);
}
};
剑指 Offer 28. 对称的二叉树
最新推荐文章于 2023-02-20 10:29:14 发布