力扣刷题记录-翻转二叉树的各种方法

这篇主要借用力扣 226. 翻转二叉树来回顾前面学到的二叉树的遍历方法,包括递归法、前序迭代法、迭代统一写法、层序遍历法。

题目如下:
在这里插入图片描述

递归法(前中后序)

前序和后序递归可以直接调换代码位置,如果用中序的递归,注意经过中间的翻转,左右孩子已经互换了,所以后面要翻转的应该还是root.left

//前序递归方法,修改一下交换代码和递归的代码的顺序就可以变成后序递归
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return root;//退出递归条件
        //以下3行负责交换
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
        //递归翻转
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

//中序递归翻转
class Solution {
    public 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);//已经互换过了,所以还是left
        return root;
    }
}

迭代法

//前序迭代版本
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return null;
        Stack<TreeNode> stk= new Stack<>();
        TreeNode node=new TreeNode();//暂存出栈结点
        TreeNode temp=new TreeNode();//用于交换
        stk.push(root);
        while(!stk.isEmpty()){
            node=stk.pop();
            temp=node.left;
            node.left=node.right;
            node.right=temp;
            if(node.right!=null)stk.push(node.right);
            if(node.left!=null)stk.push(node.left);
        }
        return root;
    }
}

//中序迭代版本
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return null;
        Stack<TreeNode> stk= new Stack<>();
        TreeNode node=new TreeNode();//暂存出栈结点
        TreeNode temp=new TreeNode();//用于交换
        stk.push(root);
        while(!stk.isEmpty()){
            node=stk.pop();
            if(node.right!=null)stk.push(node.right);
            temp=node.left;
            node.left=node.right;
            node.right=temp;
            if(node.right!=null)stk.push(node.right);
        }
        return root;
    }
}

//后序迭代版本
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return null;
        Stack<TreeNode> stk= new Stack<>();
        TreeNode node=new TreeNode();//暂存出栈结点
        TreeNode temp=new TreeNode();//用于交换
        stk.push(root);
        while(!stk.isEmpty()){
            node=stk.pop();
            if(node.right!=null)stk.push(node.right);
            if(node.left!=null)stk.push(node.left);
            temp=node.left;
            node.left=node.right;
            node.right=temp;
        }
        return root;
    }
}

迭代法(统一版本)

可以用一个模板将前中后序迭代的代码风格统一,这种模板的详细介绍在前面的文章:力扣刷题记录-二叉树的前中后序遍历(递归/迭代/迭代统一写法)里面有讲。

//迭代统一法的中序版本,只需修改if里的语句顺序就可改成前序、后序
class Solution {
    public TreeNode invertTree(TreeNode root) {
        Stack<TreeNode> stack=new Stack<>();
        if(root!=null)stack.push(root);
        TreeNode temp;
        while(!stack.isEmpty()){
            TreeNode cur=stack.peek();//cur获取栈顶元素
            if(cur!=null){
                stack.pop();
                if(cur.right!=null)stack.push(cur.right);
                stack.push(cur);
                stack.push(null);
                if(cur.left!=null)stack.push(cur.left);
            }
            else{
                stack.pop();
                cur=stack.pop();
                temp=cur.left;
                cur.left=cur.right;
                cur.right=temp;
            }
        }
        return root;
    }
}

层序遍历法

//中序+层序
class Solution {
    public TreeNode invertTree(TreeNode root) {
        Queue<TreeNode> que=new LinkedList<>();
        if(root!=null)que.offer(root);
        TreeNode temp;
        while(!que.isEmpty()){
            TreeNode cur=que.poll();
            if(cur.left!=null)que.offer(cur.left);
            temp=cur.left;
            cur.left=cur.right;
            cur.right=temp;
            if(cur.left!=null)que.offer(cur.left);
        }
        return root;
    }
}

//前序+层序
class Solution {
    public TreeNode invertTree(TreeNode root) {
        Queue<TreeNode> que=new LinkedList<>();
        if(root!=null)que.offer(root);
        TreeNode temp;
        while(!que.isEmpty()){
            TreeNode cur=que.poll();
            temp=cur.left;
            cur.left=cur.right;
            cur.right=temp;
            if(cur.left!=null)que.offer(cur.left);
            if(cur.right!=null)que.offer(cur.right);
        }
        return root;
    }
}

//后序+层序
class Solution {
    public TreeNode invertTree(TreeNode root) {
        Queue<TreeNode> que=new LinkedList<>();
        if(root!=null)que.offer(root);
        TreeNode temp;
        while(!que.isEmpty()){
            TreeNode cur=que.poll();
            if(cur.left!=null)que.offer(cur.left);
            if(cur.right!=null)que.offer(cur.right);
            temp=cur.left;
            cur.left=cur.right;
            cur.right=temp;   
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于力扣刷题C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷题中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷题中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷题中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷题中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷题中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷题中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷题中,使用 string 可以方便地处理字符串相关的问题。 9. 注意边界条件:在力扣刷题中,边界条件往往是解决问题的关键。需要仔细分析题目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷题中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值