LeetCode 226:翻转二叉树

自己写的递归的解法,还是比较高效的。 

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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值