题目描述
原题链接:226. 翻转二叉树
解题思路
分别用先、中、后序的递归遍历和DFS非递归非递归遍历和BFS层次遍历实现。
递归方式
最适合用的是先序遍历和后序遍历,遍历到一个结点就翻转其左右子树。
中序遍历的实现中,因为先遍历左子树,然后翻转,再遍历右子树。因为此时的右子树因为翻转已经变成了原来的左子树,而现在的左子树才是原来的右子树。因此应该在最后继续发展左子树。
非递归方式
对于先、中、后序,使用一个栈来模拟,每遍历到一个结点,就反转左右子树并将左右子树压入栈中进行后续遍历。其中中序遍历的处理方式与递归方式相同,最后压入的也是左子树(原右子树)。
层次遍历,使用一个队列来进行BFS,每遍历到一层的一个结点,就翻转其左右子树。
1、先序遍历
(1) 递归
/**
* 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:
// 先序遍历翻转
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
swap(root->left, root->right); // 这里要用swap,如果直接指向,会让一侧信息没有被保存
invertTree(root->left);
invertTree(root->right);
return root;
}
};
(2)迭代:遍历一层操作一次(DFS)
/**
* 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:
// 先序遍历翻转
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
if(node->left) st.push(node->left);
if(node->right) st.push(node->right);
}
return root;
}
};
2、中序遍历
(1)递归
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
invertTree(root->left);
swap(root->left, root->right); // 此时root->left已经变为root->right
invertTree(root->left); // 遍历顺序应为原root->right
return root;
}
};
(2)迭代
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top();
st.pop();
if(node->left) st.push(node->left); // 此时root->left已经变为root->right
swap(node->left, node->right);
if(node->left) st.push(node->left); // 遍历顺序应为原root->right
}
return root;
}
};
3、后序遍历
(1)递归
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
// 与left先压和right先压结果都一样,因为不包含需要与压栈顺序相关的返回,只用执行swap即可
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
return root;
}
};
(2)迭代:遍历一层操作一次(DFS)
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL) return root;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode* node = st.top();
st.pop();
if(node->left) st.push(node->left);
if(node->right) st.push(node->right);
swap(node->left, node->right);
}
return root;
}
};
4、层次遍历(BFS)
/**
* 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:
// 使用队列按层翻转每个子树
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int n = que.size();
while(n--) {
TreeNode* node = que.front();
que.pop();
swap(node->left, node->right);
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return root;
}
};
参考文章:226.翻转二叉树