自己写的递归的解法,还是比较高效的。
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == nullptr) return nullptr;
if(!root->left && !root->right) return root; //这句判断没有意义,可删去
TreeNode* temp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(temp);
return root;
}
};
看完讨论区后自己写的迭代的解法:辅助数据结构用队列和栈都可以
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
TreeNode* cur = q.front();
TreeNode* temp = cur->left;
cur->left = cur->right;
cur->right = temp;
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
q.pop();
}
return root;
}
};
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()) {
TreeNode* cur = s.top();
TreeNode* temp = cur->left;
cur->left = cur->right;
cur->right = temp;
s.pop();
if(cur->left) s.push(cur->left);
if(cur->right) s.push(cur->right);
}
return root;
}
};
讨论区优秀题解:
1 - 前中后序遍历
TreeNode* invertTree(TreeNode* root) {//先序优先遍历
if(root==NULL) return root;
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
return root;
}
TreeNode* invertTree(TreeNode* root) {//后序优先遍历
if(root==NULL) return root;
invertTree(root->left);
invertTree(root->right);
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
return root;
}
TreeNode* invertTree(TreeNode* root) {//中序优先遍历
if(root==NULL) return root;
invertTree(root->left);
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
return root;
}
作者:vailing
链接:https://leetcode-cn.com/problems/invert-binary-tree/solution/qian-zhong-hou-bian-li-by-vailing/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
2 - 非递归方法:利用队列或栈辅助实现
class Solution {
public:
TreeNode* invertTree(TreeNode* root)
{
//这是栈的解法
if(root==NULL) return NULL;//判断root是不是空,空直接返回;
stack<TreeNode *> s;//创建一个TreeNode*类型的栈;
s.push(root);
while(!s.empty())//如果栈不空就一直执行;
{
TreeNode* current=s.top();//创建一个变量储存栈顶元素;
TreeNode* temp=current->right;
current->right=current->left;
current->left=temp;
s.pop();//将栈顶元素出栈;
if(current->left!=NULL) s.push(current->left);
if(current->right!=NULL) s.push(current->right);
}
return root;
//这里开始是队列方法,和用栈的方法基本一致
if(root==NULL) return NULL;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
TreeNode* current=q.front();
TreeNode* temp=NULL;
temp=current->left;
current->left=current->right;
current->right=temp;
q.pop();
if(current->left!=NULL) q.push(current->left);
if(current->right!=NULL) q.push(current->right);
}
return root;
//这里开始是用递归的方法
if(root==NULL)return NULL;
TreeNode* right=invertTree(root->right);//创建一个right变量储存右子树翻转的结果;
TreeNode* left=invertTree(root->left);//创建一个left变量储存左子树的翻转结果;
root->left=right;//将翻转好的右子树赋值给根节点的左子树
root->right=left;//将翻转好的左子树赋值给根节点的右子树
return root;
}
};
作者:jian-han-qu-wan
链接:https://leetcode-cn.com/problems/invert-binary-tree/solution/czhan-dui-lie-di-gui-jie-fa-by-jian-han-qu-wan/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。