1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <algorithm> class Solution { public: TreeNode *invertTree(TreeNode *root) { if (root == nullptr) { return nullptr; } if (root->left == nullptr && root->right == nullptr) { return root; } std::swap(root->left, root->right); invertTree(root->left); invertTree(root->right); return root; } };
|