力扣面试150题-- 翻转二叉树

Day 41

题目描述

做法

java 复制代码
/**
 * Definition for a binary tree node.
 * public 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;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode left=invertTree(root.left);
        TreeNode right=invertTree(root.right);
        TreeNode x=left;
        root.left=right;
        root.right=left;
        return root;
    }
}
相关推荐
wuweijianlove1 分钟前
算法调度问题中的代价模型与优化方法的技术5
算法
Dxy12393102166 分钟前
Python路径算法简介
开发语言·python·算法
And_Ii31 分钟前
LCR 132.砍竹子Ⅱ
算法
汀、人工智能31 分钟前
[特殊字符] 第67课:跳跃游戏II
数据结构·算法·数据库架构·图论·bfs·跳跃游戏ii
Baihai_IDP31 分钟前
微软多模态推理模型 Phi-4-reasoning-vision 训练经验分享
人工智能·面试·llm
前端Hardy37 分钟前
前端开发效率翻倍:15个超级实用的工具函数,直接复制进项目(建议收藏)
前端·javascript·面试
Little At Air1 小时前
LeetCode 30. 串联所有单词的子串 | 困难 C++实现
算法·leetcode·职场和发展
手握风云-1 小时前
优选算法的层序之径:队列专题
数据结构·算法·leetcode
6Hzlia1 小时前
【Hot 100 刷题计划】 LeetCode 74. 搜索二维矩阵 | C++ 二分查找 (一维展开法)
c++·leetcode·矩阵