-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0226.cpp
98 lines (93 loc) · 1.73 KB
/
0226.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <deque>
#include <stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
// 递归
TreeNode* invertTree(TreeNode* root)
{
Reverse(root);
return root;
}
// 1. 递归参数和返回值 应该是先序遍历
void Reverse(TreeNode* cur)
{
// 2. 终止条件
if (cur == NULL)
return;
// 3. 单次递归处理
if (cur->left != NULL || cur->right != NULL) // 中
{
TreeNode* temp = cur->left;
cur->left = cur->right;
cur->right = temp;
}
Reverse(cur->left); // 左
Reverse(cur->right); // 右
}
// 层序遍历实现
TreeNode* invertTree(TreeNode* root)
{
deque<TreeNode*> que;
if (root != NULL)
que.push_back(root);
while (!que.empty())
{
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop_front();
TreeNode* temp = node->right;
node->right = node->left;
node->left = temp;
if (node->left != NULL)
que.push_back(node->left);
if (node->right != NULL)
que.push_back(node->right);
}
}
return root;
}
// 迭代法实现(统一)先序遍历 空结点作为标记
TreeNode* invertTree(TreeNode* root)
{
stack<TreeNode*> st;
if (root != NULL)
st.push(root);
while (!st.empty())
{
TreeNode* node = st.top();
// 还未访问
if (node != NULL)
{
st.pop();
if (node->right != NULL) // 右
st.push(node->right);
if (node->left != NULL)
st.push(node->left);
st.push(node);
st.push(NULL);
}
// 已访问过 开始处理
else
{
st.pop();
node = st.top();
st.pop();
swap(node->left, node->right);
}
}
return root;
}
};