数组转化为二叉树

来源:互联网 发布:oracle在linux安装 编辑:程序博客网 时间:2024/04/29 02:47

题目: 数组按照前序遍历方式方式转化为二叉树


思路: 二叉树前序遍历变形


代码:

Node * BinaryInit(int *a, int index, int len)
{
    if (index > len - 1)
        return NULL;
    Node *pNode = (Node *)malloc(sizeof(Node));
    pNode->val_ = a[index];
    pNode->pLeft = BinaryInit(a, 2*index+1, len);
    pNode->pRight = BinaryInit(a, 2*index+2, len);
    return pNode;
}

0 0
原创粉丝点击