翻转二叉树 (Invert Binary Tree)

 

思路:

// @Title: 翻转二叉树 (Invert Binary Tree)
// @Author: qisiii
// @Date: 2024-09-15 00:22:41
// @Runtime: 0 ms
// @Memory: 40.2 MB
// @comment: 
// @flag: 
/**
 * 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=root.left;
        TreeNode right=root.right;
        root.left=invertTree(right);
        root.right=invertTree(left);
        return root;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18