题目:
解答:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool isSymmetric(TreeNode* root) 13 { 14 if (NULL == root) 15 { 16 return true; 17 } 18 19 return helper(root->left, root->right); 20 21 } 22 23 bool helper(TreeNode *a, TreeNode *b) 24 { 25 if (a == NULL && b == NULL) 26 { 27 return true; 28 } 29 30 if (a == NULL || b == NULL) 31 { 32 return false; 33 } 34 35 if (a->val != b->val) 36 { 37 return false; 38 } 39 40 return helper(a->left, b->right) && helper(a->right, b->left); 41 } 42 43 };