-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0101-Symmetric_Tree.cpp
More file actions
155 lines (145 loc) · 3.41 KB
/
0101-Symmetric_Tree.cpp
File metadata and controls
155 lines (145 loc) · 3.41 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
/*******************************************************************************
* 0101-Symmetric_Tree.cpp
* Billy.Ljm
* 13 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/symmetric-tree/
* Given the root of a binary tree, check whether it is a mirror of itself
* (i.e., symmetric around its center).
*
* ===========
* My Approach
* ===========
* We just have to traverse the tree and compare the node suitably. No tricks.
* This should have a time complexity of O(n), and a space complexity is
* O(log n), where n it the number of nodes in the tree.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
/**
* Definition for a binary tree node.
*/
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:
/**
* Checks if binary tree is symmetric
*
* @param root root of binary tree to check
*
* @return true if binary tree is symmetric, false otherwise
*/
bool isSymmetric(TreeNode* root) {
return isMirror(root->left, root->right);
}
private:
/**
* Check if two binary trees are mirror images of each other
*
* @param root1 root of binary tree to compare against root2
* @param root2 root of binary tree to compare against root1
*
* @return true if both trees are mirror images, false otherwise
*/
bool isMirror(TreeNode* root1, TreeNode* root2) {
// both null
if (root1 == nullptr && root2 == nullptr) {
return true;
}
// one null
if (root1 == nullptr || root2 == nullptr) {
return false;
}
// both not null
return (root1->val == root2->val) &&
isMirror(root1->left, root2->right) &&
isMirror(root1->right, root2->left);
}
};
/**
* Create binary tree from vector
*
* @param vec vector to create binary tree from
*
* @return root of created binary tree
*/
TreeNode* createTree(std::vector<int> vec) {
if (vec.empty()) {
return nullptr;
}
TreeNode* root = new TreeNode(vec[0]);
std::queue<TreeNode*> q;
q.push(root);
int i = 1;
while (!q.empty() && i < vec.size()) {
TreeNode* node = q.front();
q.pop();
if (vec[i] != -1) {
node->left = new TreeNode(vec[i]);
q.push(node->left);
}
i++;
if (i < vec.size() && vec[i] != -1) {
node->right = new TreeNode(vec[i]);
q.push(node->right);
}
i++;
}
return root;
}
/**
* Deletes a binary tree
*
* @param root root of the binary tree
*/
void deleteTree(TreeNode* root) {
if (root == nullptr) {
return;
}
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
/**
* Print function for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
os << "[";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<int> vec;
TreeNode* root;
// test case 1
vec = { 1, 2, 2, 3, 4, 4, 3 };
root = createTree(vec);
std::cout << std::boolalpha << vec << ": " << sol.isSymmetric(root)
<< std::endl;
deleteTree(root);
// test case 2
vec = { 1, 2, 2, NULL, 3, NULL, 3 };
root = createTree(vec);
std::cout << std::boolalpha << vec << ": " << sol.isSymmetric(root)
<< std::endl;
deleteTree(root);
}