题解 | #二叉搜索树与双向链表#

二叉搜索树与双向链表

http://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5

typedef struct TreeNode Node;

Node* Convert_core(Node* pHead) {

    Node* pNode = pHead;
    Node* left=NULL;
    Node* right=NULL;
    Node* ret;
    if (!pNode)
        return NULL;

    if (pNode->left)
    {
        left = pNode->left;
        while (left->right)
            left = left->right;
    }
        
    
    ret = pNode;
    while (ret->left)
        ret = ret->left;
    Convert_core(pNode->left);

    if (left)
    {
        pNode->left = left;
        left->right = pNode;
    }
    
    if (pNode->right)
    {
        right = pNode->right;
        while (right->left)
            right = right->left;
    }
    Convert_core(pNode->right);
        
    if (right)
    {
        pNode->right = right;
        right->left = pNode;
    }
    return ret;
}


struct TreeNode* Convert(struct TreeNode* pRootOfTree ) {
    if (!pRootOfTree)
        return NULL;
    return Convert_core(pRootOfTree);
}
全部评论

相关推荐

点赞 评论 收藏
分享
10-21 23:48
蚌埠坦克学院
csgq:可能没hc了 昨天一面完秒挂
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务