• Algorithm --> 判读是否是子树


    问题

    判断一棵树是否是另一棵树的子树,如图

    思路

    问题分两步:

    • 找值相同的根结点(遍历解决)
    • 判断两结点是否包含(递归:值、左孩子、右孩子分别相同)

    树节点定义

    struct TreeNode
    {
        int val;
        TreeNode *next;
        TreeNode(int v) : val(v), next(NULL) {}
    };

    代码

    bool IsPart(TreeNode *root1, TreeNode *root2)
    {
        if (root2 == NULL)
            return true;
        if (root1 == NULL)
            return false;
        if (root1->val != root2->val)
            return false;
        return IsPart(root1->left, root2->left) &&
            IsPart(root1->right, root2->right);
    }
    bool IsPartTree(TreeNode *root1, TreeNode *root2)
    {
        bool result = false;
        if (root1 != NULL && root2 != NULL)
        {
            if (root1->val == root2->val)
                result = IsPart(root1, root2);
            if (!result)
                result = IsPartTree(root1->left, root2);
            if (!result)
                result = IsPartTree(root1->right, root2);
        }
        return result;
    }

    执行

    #include <iostream>
    #include <stack>
    using namespace std;
    struct TreeNode
    {
        int val;
        TreeNode *left;
        TreeNode *right;
        TreeNode(int v) : val(v), left(NULL), right(NULL) {}
    };
    bool IsPart(TreeNode *root1, TreeNode *root2)
    {
        if (root2 == NULL)
            return true;
        if (root1 == NULL)
            return false;
        if (root1->val != root2->val)
            return false;
        return IsPart(root1->left, root2->left) &&
            IsPart(root1->right, root2->right);
    }
    bool IsPartTree(TreeNode *root1, TreeNode *root2)
    {
        bool result = false;
        if (root1 != NULL && root2 != NULL)
        {
            if (root1->val == root2->val)
                result = IsPart(root1, root2);
            if (!result)
                result = IsPartTree(root1->left, root2);
            if (!result)
                result = IsPartTree(root1->right, root2);
        }
        return result;
    }
    TreeNode* createTree1()
    {
        TreeNode *root = new TreeNode(1);
        TreeNode *p1 = new TreeNode(2);
        TreeNode *p2 = new TreeNode(3);
        TreeNode *p3 = new TreeNode(4);
        TreeNode *p4 = new TreeNode(5);
        root->left = p1;
        root->right = p2;
        p2->left = p3;
        p2->right = p4;
        return root;
    }
    TreeNode* createTree2()
    {
        TreeNode *root = new TreeNode(1);
        TreeNode *p1 = new TreeNode(2);
        TreeNode *p2 = new TreeNode(3);
        root->left = p1;
        root->right = p2;
        return root;
    }
    
    
    void deleteTree(TreeNode *root)
    {
        if (root != NULL)
        {
            delete(root->left);
            delete(root->right);
            delete root;
            root = NULL;
        }
    }
    int main()
    {
        TreeNode *root1 = createTree1();
        TreeNode *root2 = createTree2();
        cout << IsPartTree(root1, root2) << endl;
        deleteTree(root1);
        deleteTree(root2);
    }
  • 相关阅读:
    一周小记(十七)
    一周小记(十九)
    2021/12/28组会
    AdaIN论文阅读(Arbitrary Style Transfer in Realtime with Adaptive Instance Normalization)
    PostgreSQL新增、更新、删除触发器
    leetcodedphard最小距离
    leetcodedp编辑距离问题两个字符串的删除操作
    正向代理代理客服端,反向代理代理服务器
    mybatisplus逻辑删除 3.1一下版本
    Eclipse使用指定JDK,无需配置Path变量
  • 原文地址:https://www.cnblogs.com/jeakeven/p/4607067.html
Copyright © 2020-2023  润新知