package tree;
public class MirrorTree {
/**
* 将一颗二叉树转换成它的镜像
* @param args
*/
public static void convertmirror(TreeNode root){
if(root==null||(root.left==null&&root.right==null)) return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
convertmirror(root.left);
convertmirror(root.right);
}
public static void printf(TreeNode root){
if(root==null) return;
System.out.print(root.value+" ");
printf(root.left);
printf(root.right);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(3);
root.right = new TreeNode(2);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(4);
printf(root);
System.out.println();
convertmirror(root);
printf(root);
}
}
将一颗二叉树转换成它的镜像
最新推荐文章于 2021-04-07 11:13:22 发布