剑指offer:合并两个排序的链表&树的子结构&二叉树的镜像

16.合并两个排序的链表

/**************************************************************/
/* 题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。 */
/**************************************************************/

class Solution {
public:
    ListNode * Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if (pHead1 == NULL && pHead2 != NULL)
            return pHead2;
        if (pHead2 == NULL && pHead1 != NULL)
            return pHead1;
        if (pHead2 == NULL && pHead1 == NULL)
            return NULL;
        ListNode *pNewHead = NULL;
        if (pHead1->val > pHead2->val)
        {
            pNewHead = pHead2;
            pHead2 = pHead2->next;
        }
        else
        {
            pNewHead = pHead1;
            pHead1 = pHead1->next;
        }
        ListNode *current = pNewHead;
        while (pHead1 != NULL && pHead2 != NULL)
        {
            if (pHead1->val > pHead2->val)
            {
                current->next = pHead2;
                pHead2 = pHead2->next;
                current = current->next;
            }
            else
            {
                current->next = pHead1;
                pHead1 = pHead1->next;
                current = current->next;
            }
        }
        if (pHead1 != NULL)
        {
            current->next = pHead1;
        }
        if (pHead2 != NULL)
            current->next = pHead2;
        return pNewHead;
    }
};

17.树的子结构

/************************************************************************/
/* 题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。
(ps:我们约定空树不是任意一个树的子结构)                                     */
/************************************************************************/
bool isHasB(TreeNode* pRoot1, TreeNode* pRoot2)
{
    if (pRoot2 == NULL)
        return true; //pRoot2为空说明前面的都匹配了
    if (pRoot1 == NULL)
        return false; //pRoot1为空,但是pRoot2不为空,说明没有全匹配
    if (pRoot1->val != pRoot2->val)
        return false;
    //继续判断下一个结点
    return isHasB(pRoot1->left, pRoot2->left) && isHasB(pRoot1->right, pRoot2->right);
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
    bool result = false;
    if (pRoot1 == NULL || pRoot2 == NULL)
        return false;
    if (pRoot1->val == pRoot2->val)
    {
        result = isHasB(pRoot1, pRoot2);
    }
    //不匹配,继续遍历
    if (!result)
    {
        result = HasSubtree(pRoot1->left, pRoot2);
    }
    if (!result)
    {
        result = HasSubtree(pRoot1->right, pRoot2);
    }
    return result;
}

18.二叉树的镜像

/************************************************************************/
/* 题目描述
操作给定的二叉树,将其变换为源二叉树的镜像                                                                     */
/************************************************************************/
void Mirror(TreeNode *pRoot) {
    if (pRoot == NULL)
        return;
    TreeNode *tmp = pRoot->left;
    pRoot->left = pRoot->right;
    pRoot->right = tmp;
    Mirror(pRoot->left);
    Mirror(pRoot->right);
}

猜你喜欢

转载自blog.csdn.net/foreverdongjian/article/details/82254668