题目描述 操作给定的二叉树,将其变换为源二叉树的镜像。 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5 解题思路: 利用递归!
public class Solution24 { public static class TreeNode{ int val; TreeNode left = null; TreeNode right = null; public TreeNode(int val){ this.val = val; } public TreeNode(int val,TreeNode val1,TreeNode val2){ this.val = val; left = val1; right = val2; } } public static void Mirror(TreeNode root){ TreeNode tmp = null; if (root != null){ tmp = root.left; root.left = root.right; root.right = tmp; } if (root.left != null){ Mirror(root.left); } if (root.right != null){ Mirror(root.right); } System.out.println(root.val+" "+root.left+ " "+root.right); } public static void main(String args[]){ TreeNode a = new TreeNode(5); Mirror(a); } }