二分木ミラー変換
問題の説明:指定されたバイナリツリーを操作し、それをソースバイナリツリーのミラーイメージに変換します。
コード:
package hgy.java.arithmetic;
public class BinaryTreeMirror {
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public void mirror(TreeNode root) {
if(root == null)
return ;
change(root);
mirror(root.left);
mirror(root.right);
}
//交换结点左右子树
public void change(TreeNode root){
TreeNode node = root.left;
root.left = root.right;
root.right = node;
}
}