码迷,mamicode.com
首页 > 其他好文 > 详细

前序中序构建二叉树

时间:2017-05-24 21:10:39      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:rem   ret   length   sharp   nod   i++   二叉树   csharp   ==   

    public Node PreMidToTree(int[] pre,int[] mid)
    {
        if (pre == null || mid == null)
            return;
        Dictionary<int, int> dic = new Dictionary<int, int>();
        for (int i = 0; i < mid.Length; i++)
            dic[mid[i]] = i;
        return PreMid(pre, 0, pre.Length - 1, mid, 0, mid.Length - 1, dic);

    }

    private Node PreMid(int[] pre, int ps, int pe, int[] mid, int ms, int me, Dictionary<int, int> dic)
    {
        if (ps > pj)
            return null;
        Node head = new Node(p[pi]);
        int index = dic[pre[ps]];
        head.left = PreMid(pre,ps+1,ps+index-ms,mid,ms,index-1,dic);
        head.right = PreMid(pre, ps + 1, ps + index - ms, mid, ms, index - 1, dic);
        return head;
    }

  

前序中序构建二叉树

标签:rem   ret   length   sharp   nod   i++   二叉树   csharp   ==   

原文地址:http://www.cnblogs.com/binglangwu/p/6900749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!