标签:
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
Note:
Bonus points if you could solve it both recursively and iteratively.
[Solution]
递归判断
symCheck(left->left, right->right) && symCheck(left->right, right->left)
1 bool isSymmetric(TreeNode *root) 2 { 3 if (root == NULL) 4 return true; 5 return symCheck(root->left, root->right); 6 } 7 8 bool symCheck(TreeNode *left, TreeNode *right) 9 { 10 if (left == NULL || right == NULL) 11 { 12 if (left == right) 13 return true; 14 else 15 return fasle; 16 } 17 18 if (left->val == right->val) 19 return symCheck(left->left, right->right) && symCheck(left->right, right->left); 20 else 21 return false; 22 }
标签:
原文地址:http://www.cnblogs.com/ym65536/p/4295360.html