0951.cpp (441B)
1 class Solution { 2 public: 3 bool flipEquiv(const TreeNode *root1, const TreeNode *root2) const { 4 if (!root1 && !root2) return true; 5 if (!root1 || !root2) return false; 6 if (root1->val != root2->val) return false; 7 8 return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)) || 9 (flipEquiv(root1->right, root2->left) && flipEquiv(root1->left, root2->right)); 10 } 11 };