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

本文详细介绍了如何使用递归和迭代方法实现二叉树的前序、中序和后序遍历,以及如何通过统一模板处理三种遍历方式。重点展示了翻转二叉树的过程,并提供了层序遍历的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇主要借用力扣 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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值