package java_study.JianZhiOffer;
import org.junit.Test;
/**
* Created by ethan on 2015/6/29.
* 剑指offer No19 二叉树的镜像
*/
public class No19二叉树的镜像 {
public void toMirror(TreeNode root){
if (root==null) return;
if (root.getLchild()==null && root.getRchild()==null) return;
TreeNode tmp = root.getLchild();
root.setLchild(root.getRchild());
root.setRchild(tmp);
if (root.getLchild()!=null)
toMirror(root.getLchild());
if (root.getRchild()!=null)
toMirror(root.getRchild());
}
public TreeNode init(){
String preStr = "abdhiegkcfg" ;
String inStr = "hdibgekafcg";
TreeNode root = TreeUtil.buildTree(preStr, inStr);
TreeUtil.postOrderNoRecursive(root);
return root;
}
@Test
public void test1(){
TreeNode root = init();
toMirror(root);
TreeUtil.postOrder(root);
System.out.println();
TreeUtil.inOrder(root);
System.out.println();
TreeUtil.preOrder(root);
System.out.println();
}
}
二叉树的镜像
最新推荐文章于 2015-06-29 11:56:57 发布