-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27_MirrorTree.cpp
More file actions
164 lines (122 loc) · 3.23 KB
/
27_MirrorTree.cpp
File metadata and controls
164 lines (122 loc) · 3.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Created by mark on 2019/7/9.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:27.二叉树镜像,镜像输出二叉树
2. 思路:镜像就是交换每个根节点的左右子节点,遍历二叉树,然后不断交换;
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
struct BinaryTreeNode
{
int val;
BinaryTreeNode* left;
BinaryTreeNode* right;
}TN,*pTN;
// 1. 递归
void Mirror_Recu(BinaryTreeNode* root)
{
if(root == nullptr)
return;
if(root->left == nullptr && root->right == nullptr)
return ;
BinaryTreeNode* temp = root->left; // 交换左右子节点
root->left = root->right;
root->right = temp;
if(root->left != nullptr) // 交换左右子节点
Mirror_Recu(root->left);
if(root->right != nullptr)
Mirror_Recu(root->right);
}
// 2. 非递归,先序遍历
void Mirror_iter(BinaryTreeNode* root)
{
if(root == nullptr)
return;
stack<BinaryTreeNode*> s;
s.push(root);
while(!s.empty())
{
BinaryTreeNode* cur = s.top();
s.pop();
swap(cur->left, cur->right); // 交换当前根节点的左右子树节点
if(cur->left != nullptr)
s.push(cur->left);
if(cur->right != nullptr)
s.push(cur->right);
}
}
//辅助函数 ------------------------------------------------------------------------------------------------------------------------
// 构建树节点
BinaryTreeNode* CreateTreeNode(int val)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->val = val;
pNode->left = nullptr;
pNode->right = nullptr;
return pNode;
}
// 连接树节点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != nullptr)
{
pParent->left = pLeft;
pParent->right = pRight;
}
}
// 销毁树
void DestroyTree(BinaryTreeNode* root)
{
if(root != nullptr)
{
BinaryTreeNode* left = root->left;
BinaryTreeNode* right = root->right;
delete root;
root = nullptr;
DestroyTree(left);
DestroyTree(right);
}
}
// 先序打印树
void PrintPreOrder(BinaryTreeNode* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
PrintPreOrder(root->left);
PrintPreOrder(root->right);
}
int main(){
// 创建个树
/*
1
2 3
4 5 6 7
*/
BinaryTreeNode* A1 = CreateTreeNode(1);
BinaryTreeNode* A2 = CreateTreeNode(2);
BinaryTreeNode* A3 = CreateTreeNode(3);
BinaryTreeNode* A4 = CreateTreeNode(4);
BinaryTreeNode* A5 = CreateTreeNode(5);
BinaryTreeNode* A6 = CreateTreeNode(6);
BinaryTreeNode* A7 = CreateTreeNode(7);
ConnectTreeNodes(A1, A2, A3);
ConnectTreeNodes(A2, A4, A5);
ConnectTreeNodes(A3, A6, A7);
cout << "原树是:";
PrintPreOrder(A1);
cout << endl;
cout << "镜像后:";
Mirror_iter(A1);
PrintPreOrder(A1);
cout << endl;
return 0;
}