1 class Solution01 { 2 public: 3 bool isSymmetrical(TreeNode* pRoot) 4 { 5 if (pRoot == nullptr)return true; 6 vector<int>v1, v2; 7 DFS(pRoot, v1, true); 8 DFS(pRoot, v2, false); 9 if (v1 == v2)return true; 10 else return false; 11 12 } 13 void DFS(TreeNode* pRoot, vector<int>&v,const bool flag) 14 { 15 if (pRoot == nullptr) 16 v.push_back(-1); 17 else 18 v.push_back(pRoot->val); 19 if (pRoot == nullptr)return; 20 if (flag) 21 { 22 DFS(pRoot->left, v, flag); 23 DFS(pRoot->right, v, flag); 24 } 25 else 26 { 27 DFS(pRoot->right, v, flag); 28 DFS(pRoot->left, v, flag); 29 } 30 } 31 }; 32 33 class Solution02 { 34 public: 35 bool isSymmetrical(TreeNode* pRoot) 36 { 37 return DFS(pRoot, pRoot); 38 39 } 40 bool DFS(TreeNode* root1, TreeNode* root2) 41 { 42 if (root1 == nullptr && root2 == nullptr)return true; 43 if (root1 == nullptr || root2 == nullptr)return false; 44 if (root1->val != root2->val)return false; 45 return DFS(root1->left, root2->right) && DFS(root1->right, root2->left); 46 } 47 };
原文:https://www.cnblogs.com/zzw1024/p/11674209.html