forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
31 lines (29 loc) · 861 Bytes
/
solution.h
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
#include <cstddef>
#include <stack>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if (root == NULL) return true;
TreeNode *lt = root->left, *rt = root->right;
for (std::stack<TreeNode*> stack; !stack.empty() || (lt&&rt); ) {
if (lt && rt) {
if (lt->val != rt->val) return false;
stack.push(lt->right);stack.push(rt->left);
lt = lt->left; rt = rt->right;
}
else if (lt || rt) return false;
else {
lt = stack.top(); stack.pop();
rt = stack.top(); stack.pop();
}
}
if (lt || rt) return false;
else return true;
}
};