【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());
    }
}
相关推荐
用户044144092449几秒前
卫星信号模拟器里的四个坐标转换公式
算法
橘和柠10 分钟前
阿里 open-code-review (AI代码审查工具)实战:安装、四层规则链、自定义规则格式与实测避坑
算法·面试
得物技术10 分钟前
别再只卷向量检索了,得物交易搜索如何用“生成式”实现召回范式跃迁?
人工智能·算法·llm
罗西的思考11 分钟前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(4)--- Rollout实现细节
人工智能·算法
罗西的思考22 分钟前
机器人 / 物理 Agent Harness 综合分析与对比:从「更强的模型」到「更好的系统」
人工智能·算法·机器学习
库玛西23 分钟前
攻克 408 数据结构:图论基石深度拆解(数学极值推演 + 存储内存剖析 + BFS/DFS 双核模板)
数据结构·算法·深度优先·广度优先·图搜索算法
倒头就睡的小比特5 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼5 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控