37_2 反序列化二叉树

package sort;

public class Test37_2 {
    public static int index = 0;

    public static void main(String[] args) {
        String test = "124$$$35$$6$$";
        Test37.printTree(DisSerialied(test));
    }

    public static BinayTreeNode DisSerialied(String str) {

        char[] targe = str.toCharArray();
        BinayTreeNode root = doDisSerialided(targe, index);
        return root;
    }

    private static BinayTreeNode doDisSerialided(char[] targe, int i) {
        // TODO Auto-generated method stub
        if (i >= targe.length || targe[i] == '$')//递归出口
            return null;

        BinayTreeNode temp = new BinayTreeNode(Integer.parseInt(targe[i] + ""),
                null, null);
       //根左右的顺序进行建树
        temp.left = doDisSerialided(targe, ++index);//index为全局变量
        temp.right = doDisSerialided(targe, ++index);
        return temp;

    }
}
 

发布了41 篇原创文章 · 获赞 1 · 访问量 755

猜你喜欢

转载自blog.csdn.net/coder_my_lover/article/details/105297006