博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
牛客OJ:判断子结构和子树
阅读量:4059 次
发布时间:2019-05-25

本文共 1355 字,大约阅读时间需要 4 分钟。

判断子结构:

class Solution {public:    bool solve(TreeNode* a, TreeNode* b){        if(b == NULL) return true;        if(a == NULL) return false;        if(a->val == b->val){            return                 solve(a->left,b->left)                &&                 solve(a->right,b->right);        }        return false;    }    bool judge(TreeNode* a,TreeNode* b){        if(a == NULL || b == NULL) return false;        bool ans = false;        if(a->val == b->val) ans = (ans || solve(a,b));        if(ans) return true;        ans = (ans || judge(a->left,b));        ans = (ans || judge(a->right,b));        return ans;    }    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)    {        return judge(pRoot1,pRoot2);    }};

判断子树:

bool solve(TreeNode* a, TreeNode* b){    if(a == NULL && b == NULL) return true;    if(a == NULL || b == NULL) return false;    if(a->val == b->val){        return             solve(a->left,b->left)            &&             solve(a->right,b->right);    }    return false;}bool judge(TreeNode* a,TreeNode* b){    if(a == NULL || b == NULL) return false;    bool ans = false;    if(a->val == b->val) ans = (ans || solve(a,b));    if(ans) return true;    ans = (ans || judge(a->left,b));    ans = (ans || judge(a->right,b));    return ans;}bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2){    return judge(pRoot1,pRoot2);}

转载地址:http://mfwji.baihongyu.com/

你可能感兴趣的文章
[关注大学生]李开复给中国计算机系大学生的7点建议
查看>>
[茶余饭后]10大毕业生必听得歌曲
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>