递归
前序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
return root;
}
};
中序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
invertTree(root->left);
swap(root->left, root-> right);
invertTree(root->left);
return root;
}
};
后序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
return root;
}
};
迭代
前序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
swap(cur->left, cur->right);
if (cur->left) st.push(cur->left);
if (cur->right)st.push(cur->right);
}
return root;
}
};
统一迭代前序
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
stack<TreeNode*> st;
st.push(root);
while (!st.empty()) {
TreeNode *cur = st.top();
st.pop();
if (nullptr != cur) {
if (cur->right)st.push(cur->right);
if (cur->left) st.push(cur->left);
st.push(cur);
st.push(nullptr);
} else {
cur = st.top();
st.pop();
swap(cur->left, cur->right);
}
}
return root;
}
};
层序遍历
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (nullptr == root) return root;
queue<TreeNode*> que;
que.push(root);
while (!que.empty()) {
int size = que.size();
for (int i = 0; i < size; i++) {
TreeNode*cur = que.front();
que.pop();
swap(cur->left, cur->right);
if (cur->left) que.push(cur->left);
if (cur->right)que.push(cur->right);
}
}
return root;
}
};
递归
class Solution {
public:
bool traversal(TreeNode *left, TreeNode *right) {
if (nullptr == left && nullptr != right) return false;
else if (nullptr != left && nullptr == right) return false;
else if (nullptr == left && nullptr == right) return true;
else if (left->val != right->val) return false;
bool outside = traversal(left->left, right->right);
bool inside = traversal(left->right, right->left);
return outside && inside;
}
bool isSymmetric(TreeNode* root) {
if (nullptr == root) return true;
return traversal(root->left, root->right);
}
};
使用队列迭代
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (nullptr == root) return true;
queue<TreeNode*> que;
que.push(root->left);
que.push(root->right);
while (!que.empty()) {
TreeNode* left = que.front(); que.pop();
TreeNode* right = que.front();que.pop();
if (!left && !right) {
continue;
}
if (!left || !right || (left->val != right->val)) {
return false;
}
que.push(left->left);
que.push(right->right);
que.push(left->right);
que.push(right->left);
}
return true;
}
};
使用栈迭代
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (nullptr == root) return true;
stack<TreeNode*> st;
st.push(root->left);
st.push(root->right);
while (!st.empty()) {
TreeNode *left = st.top(); st.pop();
TreeNode *right = st.top();st.pop();
if (!left && !right) continue;
if (!left || !right || (left->val != right->val)) return false;
st.push(left->left);
st.push(right->right);
st.push(left->right);
st.push(right->left);
}
return true;
}
};