226. 翻转二叉树
为保证权益,题目请参考 226. 翻转二叉树(From LeetCode).
解决方案1
CPP
C++
/*
* LeetCode 226
*
* Date: 2020-05-03
* Author: Keven Ge
*/
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <limits.h>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *invertTree(TreeNode *root) {
if(root){
// TreeNode *tmp = root->left;
// root->left = root->right;
// root->right = tmp;
//
// root->left = invertTree(root->left);
// root->right = invertTree(root->right);
TreeNode *tmp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(tmp);
return root;
}
return NULL;
}
};
int main() {
return 0;
}
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
47
48
49
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
47
48
49