-
Notifications
You must be signed in to change notification settings - Fork 0
/
invert_binary_tree.cpp
46 lines (44 loc) · 1.17 KB
/
invert_binary_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* =====================================================================================
*
* Filename: invert_binary_tree.cpp
*
* Description: 226. Invert Binary Tree
* https://leetcode.com/problems/invert-binary-tree
*
* Version: 1.0
* Created: 10/17/2021 15:07:51
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
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) {
return root;
} else {
TreeNode* tmp = invertTree(root->right);
root->right = invertTree(root->left);
root->left = tmp;
return root;
}
}
};
int main(int argc, char* argv[]) {
TreeNode* root = nullptr;
root = Solution().invertTree(root);
return 0;
}