牛客网-二叉树的镜像

操作给定的二叉树,将其变换为源二叉树的镜像。

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null)
        {
            return;
        }
        if(root.left != null || root.right != null)
        {
            TreeNode tem = root.left;
            root.left = root.right;
            root.right = tem;
            Mirror(root.left);
            Mirror(root.right);
        }else{
            return;
        }
    }
}
发布了41 篇原创文章 · 获赞 6 · 访问量 6474

猜你喜欢

转载自blog.csdn.net/qq_42712280/article/details/104415229