mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
25
Easy/invert-binary-tree/solution.cpp
Normal file
25
Easy/invert-binary-tree/solution.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode *invertTree(TreeNode *root) {
|
||||
if (root != nullptr) {
|
||||
invertTree(root->left);
|
||||
invertTree(root->right);
|
||||
|
||||
TreeNode *copy = root->left;
|
||||
root->left = root->right;
|
||||
root->right = copy;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user