标签:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / 2 2 / \ / 3 4 4 3
But the following is not:
1 / 2 2 \ 3 3
class Solution { public: bool isSymmetric(TreeNode* root) { if(root==NULL) return true; if(root->left==NULL&&root->right==NULL) return true; return sm(root->left,root->right); } bool sm(TreeNode* left,TreeNode* right) { if(left==NULL&&right==NULL) return true; if((left==NULL&&right!=NULL)||(left!=NULL&&right==NULL)||(left->val!=right->val)) return false; return sm(left->left,right->right)&sm(left->right,right->left); } };
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/45486405