-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28_Symmetric_Tree.cpp
More file actions
178 lines (133 loc) · 4.05 KB
/
28_Symmetric_Tree.cpp
File metadata and controls
178 lines (133 loc) · 4.05 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by mark on 2019/7/8.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:27.判断一个数是不是对称
2. 思路:递归和非递归
1. 树是空,true
2. 判断左右子树是否对称:
1. 根节点相等;
2. 且左子树的左子树和右子树的右子树对称;
3. 左子树的右子树和右子树的左子树对称;
*/
#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. 递归
bool isSymmetric(BinaryTreeNode* left, BinaryTreeNode* right);
bool isSymmetric_Recu(BinaryTreeNode* root)
{
if(root == nullptr)
return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(BinaryTreeNode* left, BinaryTreeNode* right)
{
if(left == nullptr && right == nullptr) // 1.左右都为空
return true;
if(left->val != right->val) // 2.左右子节点相等
return false;
// 3.左子树的左子树要和右子树的右子树对称,同时左子树的右子树要和右子树的左子树
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
}
// 2. 迭代
bool isSymmetric_iter(BinaryTreeNode* root)
{
if(root == nullptr)
return true;
stack<BinaryTreeNode*> s;
BinaryTreeNode* left = root->left;
BinaryTreeNode* right = root->right;
s.push(left);
s.push(right);
while(!s.empty())
{
BinaryTreeNode* temp1 = s.top();s.pop();
BinaryTreeNode* temp2 = s.top();s.pop();
if(temp1 == nullptr && temp2 == nullptr) continue; // 同时为空才行
if(temp1 == nullptr || temp2 == nullptr) return false;
if(temp1->val != temp2->val) return false; // 值要相等
s.push(temp1->left); s.push(temp2->right); // 注意这个入栈顺序:左子树的左节点-右子树的右节点, 左子树的右节点-右子树的左节点
s.push(temp1->right); s.push(temp2->left);
}
return true;
}
//辅助函数 ------------------------------------------------------------------------------------------------------------------------
// 构建树节点
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 2
4 5 5 4
*/
BinaryTreeNode* A1 = CreateTreeNode(1);
BinaryTreeNode* A2 = CreateTreeNode(2);
BinaryTreeNode* A3 = CreateTreeNode(2);
BinaryTreeNode* A4 = CreateTreeNode(4);
BinaryTreeNode* A5 = CreateTreeNode(5);
BinaryTreeNode* A6 = CreateTreeNode(5);
BinaryTreeNode* A7 = CreateTreeNode(4);
ConnectTreeNodes(A1, A2, A3);
ConnectTreeNodes(A2, A4, A5);
ConnectTreeNodes(A3, A6, A7);
cout << "原树是:";
PrintPreOrder(A1);
cout << endl;
if(isSymmetric_iter(A1))
cout << "对称树" << endl;
else
cout << "非对称树" << endl;
return 0;
}