【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());
    }
}
相关推荐
QiZhang | UESTC18 小时前
JAVA算法练习题day11
java·开发语言·python·算法·hot100
屁股割了还要学18 小时前
【数据结构入门】排序算法(4)归并排序
c语言·数据结构·学习·算法·排序算法
Tisfy18 小时前
LeetCode 0966.元音拼写检查器:三个哈希表实现
leetcode·字符串·散列表·题解·哈希表
努力学习的小廉18 小时前
我爱学算法之—— 位运算(上)
c++·算法
ゞ 正在缓冲99%…19 小时前
leetcode35.搜索插入位置
java·算法·leetcode·二分查找
lifallen19 小时前
字节跳动Redis变种Abase:无主多写架构如何解决高可用难题
数据结构·redis·分布式·算法·缓存
feifeigo12319 小时前
星座SAR动目标检测(GMTI)
人工智能·算法·目标跟踪
WWZZ202519 小时前
视觉SLAM第10讲:后端2(滑动窗口与位子图优化)
c++·人工智能·后端·算法·ubuntu·机器人·自动驾驶
YuTaoShao19 小时前
【LeetCode 每日一题】36. 有效的数独
linux·算法·leetcode
IT古董20 小时前
【漫话机器学习系列】003.Agglomerative聚类
人工智能·算法·机器学习