【Hot100】LeetCode—226. 翻转二叉树

目录

  • [1- 思路](#1- 思路)
    • [Queue 队列实现层序遍历 + 交换左右](#Queue 队列实现层序遍历 + 交换左右)
  • [2- 实现](#2- 实现)
    • [⭐226. 翻转二叉树------题解思路](#⭐226. 翻转二叉树——题解思路)
  • [3- ACM 实现](#3- ACM 实现)

1- 思路

Queue 队列实现层序遍历 + 交换左右

  • 1- 借助 Queue 实现层序遍历
  • 2- 实现左右交换方式

2- 实现

⭐226. 翻转二叉树------题解思路

java 复制代码
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return root;
        }
        // 借助队列实现层序交换
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            while(len>0){
                TreeNode nowNode = queue.poll();
                swapNode(nowNode);
                if(nowNode.left!=null) queue.add(nowNode.left);
                if(nowNode.right!=null) queue.add(nowNode.right);
                len--;
            }
        }
        return root;
    }

    public void swapNode(TreeNode node){
        TreeNode tmp = node.left;
        node.left = node.right;
        node.right = tmp;
    }
}

3- ACM 实现

java 复制代码
public class invertTree {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }



    public static TreeNode build(String str){
        if(str == null || str.length()==0){
            return null;
        }
        String input = str.replace("[","");
        input = input.replace("]","");
        String[] parts = input.split(",");

        Integer[] nums = new Integer[parts.length];
        for(int i = 0 ; i < parts.length;i++){
            if(!parts[i].equals("null")){
                nums[i] = Integer.parseInt(parts[i]);
            }else{
                nums[i] = null;
            }
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty()&& index<parts.length){
            TreeNode node = queue.poll();
            if(index<nums.length && nums[index]!=null){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(index<nums.length && nums[index]!=null){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }
    public static TreeNode invertTree(TreeNode root) {
        if(root==null){
            return root;
        }
        // 借助队列实现层序交换
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            while(len>0){
                TreeNode nowNode = queue.poll();
                swapNode(nowNode);
                if(nowNode.left!=null) queue.add(nowNode.left);
                if(nowNode.right!=null) queue.add(nowNode.right);
                len--;
            }
        }
        return root;
    }

    public static void swapNode(TreeNode node){
        TreeNode tmp = node.left;
        node.left = node.right;
        node.right = tmp;
    }

    static List<Integer> levelRes = new ArrayList<>();
    public static TreeNode levelT(TreeNode root) {
        if(root==null){
            return root;
        }
        // 借助队列实现层序交换
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            while(len>0){
                TreeNode nowNode = queue.poll();
                levelRes.add(nowNode.val);
                if(nowNode.left!=null) queue.add(nowNode.left);
                if(nowNode.right!=null) queue.add(nowNode.right);
                len--;
            }
        }
        return root;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);
        root = invertTree(root);
        levelT(root);
        System.out.println("结果是"+levelRes.toString());
    }
}
相关推荐
int型码农4 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT5 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面5 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked935 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,5 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵5 小时前
有效的括号题解
数据结构·算法·
GIS小天5 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__5 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃5 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子5 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#