【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());
    }
}
相关推荐
ShiinaMashirol4 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
wuqingshun3141596 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯
Demons_kirit6 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
adam_life6 小时前
http://noi.openjudge.cn/——2.5基本算法之搜索——200:Solitaire
算法·宽搜·布局唯一码
我想进大厂7 小时前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂7 小时前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代7 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
CODE_RabbitV8 小时前
【深度强化学习 DRL 快速实践】近端策略优化 (PPO)
算法
Wendy_robot9 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode
程序员-King.9 小时前
day49—双指针+贪心—验证回文串(LeetCode-680)
算法·leetcode·贪心算法·双指针