剑指offer 学习笔记 树的子结构

面试题26:树的子结构。输入两棵二叉树A和B,判断B是不是A的子结构。二叉树节点定义如下:

struct BinaryTreeNode{
    int m_nValue;
    BinaryTreeNode *m_pLeft;
    BinaryTreeNode *m_pRight;
};

我们可以分两步解决这个问题。第一步,在树A中找到和树B的根节点的值一样的节点R。第二步,判断树A中以R为根节点的子树是不是包含和树B一样的结构:

#include <iostream>
using namespace std;

struct BinaryTreeNode {
    int m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};

BinaryTreeNode* ConstructCore(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder) {    // 创建树
    BinaryTreeNode* root = new BinaryTreeNode();
    root->m_nValue = *startPreorder;
    root->m_pLeft = root->m_pRight = nullptr;

    if (startPreorder == endPreorder) {    // 当递归到前序遍历只含一个元素时
        if (startInorder == endInorder && *startPreorder == *startInorder) {    // 此时若中序遍历也只含一个元素并且这个元素值与前序遍历的元素值相同时,递归到底成功返回
            return root;
        } else {    // 否则当前序和中序遍历的个数不相等(前序遍历只含一个元素但中序遍历有多个元素)或值不相等(前序遍历和中序遍历元素数都为1但这两个值不等)时
            throw exception("Invalid input.");    // 说明输入的前序和中序遍历不匹配
        }
    }

    int* rootInorder = startInorder;
    while (rootInorder < endInorder && *rootInorder != *startPreorder) {    // 遍历中序序列找到根节点
        ++rootInorder;
    }

    if (rootInorder == endInorder && *rootInorder != *startPreorder) {    // 若以上循环结束仍未找到根节点,说明输入有误
        throw exception("Invalid input");
    }

    int leftLength = rootInorder - startInorder;    // 左子树长度
    int* leftPreorderEnd = startPreorder + leftLength;    // 左子树尾边界
    if (leftLength > 0) {    // 当左子树仍存在时
        root->m_pLeft = ConstructCore(startPreorder + 1, leftPreorderEnd, startInorder, rootInorder - 1);    // 继续递归左子树
    }
    if (leftLength < endPreorder - startPreorder) {    // 左子树长度小于当前遍历的树节点数-1(去掉根节点)时,说明存在右子树
        root->m_pRight = ConstructCore(startPreorder + leftLength + 1, endPreorder, rootInorder + 1, endInorder);    // 继续递归右子树
    }

    return root;
}

BinaryTreeNode* Construct(int* preorder, int* inorder, int length) {    // 创建树
    if (preorder == nullptr || inorder == nullptr || length <= 0) {
        return nullptr;
    }

    return ConstructCore(preorder, preorder + length - 1, inorder, inorder + length - 1);
}

bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) {
    if (pRoot2 == nullptr) {    // pRoot2为空时说明匹配成功
        return true;
    }
    if (pRoot1 == nullptr) {    // pRoot2不为空但pRoot1为空时匹配失败
        return false;
    }

    if (pRoot1->m_nValue == pRoot2->m_nValue) {
        return DoesTree1HaveTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) && DoesTree1HaveTree2(pRoot1->m_pRight, pRoot2->m_pRight);
    } else {
        return false;
    }
}

bool HasSubtree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) {
    bool result = false;
    if (pRoot1 == nullptr || pRoot2 == nullptr) {
        return false;
    }

    if (pRoot1->m_nValue == pRoot2->m_nValue) {
        result = DoesTree1HaveTree2(pRoot1, pRoot2);
    }
    if (result == false) {
        result = HasSubtree(pRoot1->m_pLeft, pRoot2) || HasSubtree(pRoot1->m_pRight, pRoot2);
    }
    return result;
}

int main() {
    // 根据前序和中序遍历建树,树中不允许有重复数据
    int inorder1[] = { 4,2,6,5,7,1,3 };    
    int preorder1[] = { 1,2,4,5,6,7,3 };    
    BinaryTreeNode* left = Construct(preorder1, inorder1, 7);    // 较大的树

    int inorder2[] = { 4,2,5 };    
    int preorder2[] = { 2,4,5 };   
    BinaryTreeNode* right = Construct(preorder2, inorder2, 3);    // 较小的树

    cout << HasSubtree(left, right) << endl;
}

若二叉树保存的值类型为double,只需将int类型改为double类型,但在判断节点值是否相等,即两个浮点数值是否相等时,要用:

bool Equal(double num1, double num2) {
    if ((num1 - num2 > -1e-8) && (num1 - num2 < 1e-8)) {    // 1e-8的误差内认为两数相等,误差范围根据实际确定
        return true;
    } else {
        return false;
    }
}
posted @ 2020-02-29 14:58  epiphanyy  阅读(7)  评论(0)    收藏  举报  来源