剑指 Offer 27. 二叉树的镜像

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        //节点root为空,返回null
        if(root == null)
        return null;
        //交换左右节点
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        //递归
        mirrorTree(root.left);
        mirrorTree(root.right);
        
        return root;
    }
}

 

posted @ 2020-12-14 18:03  peanut_zh  阅读(20)  评论(0编辑  收藏  举报