-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0101.cpp
More file actions
33 lines (30 loc) · 797 Bytes
/
Copy path0101.cpp
File metadata and controls
33 lines (30 loc) · 797 Bytes
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
class Solution {
string preorder(TreeNode *root, bool left) {
if (!root) return "";
string res;
stack<TreeNode *> st;
st.push(root);
while (!st.empty()) {
TreeNode *root = st.top();
st.pop();
if (!root) {
res += "-";
continue;
}
res += root->val;
if (left) {
st.push(root->right);
st.push(root->left);
} else {
st.push(root->left);
st.push(root->right);
}
}
return res;
}
public:
bool isSymmetric(TreeNode *root) {
if (!root) return false;
return preorder(root->left, true) == preorder(root->right, false);
}
};