二叉树的镜像

来源:互联网 发布:java 动态替换jar 编辑:程序博客网 时间:2024/05/18 00:23
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();    }}

0 0
原创粉丝点击