2014-03-19 05:04

题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树。子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样。

解法:首先可以根据节点个数省去一大部分不必要的搜索,然后再递归判断。代码还比较简单,请看下面。

代码:

 // 4.8 Check if a tree is a subtree of another.
#include <cstdio>
#include <unordered_map>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right; TreeNode(int _val = ): val(_val), left(nullptr), right(nullptr) {};
}; void constructTree(TreeNode *&root)
{
int val; scanf("%d", &val);
if (val == ) {
root = nullptr;
} else {
root = new TreeNode(val);
constructTree(root->left);
constructTree(root->right);
}
} int countNode(TreeNode *root, unordered_map<TreeNode *, int> &node_count)
{
if (root == nullptr) {
return ;
} else {
node_count[root] = countNode(root->left, node_count) + countNode(root->right, node_count) + ;
return node_count[root];
}
} bool isIdentical(TreeNode *root1, TreeNode *root2)
{
if (root1 == nullptr) {
return root2 == nullptr;
} else if (root2 == nullptr) {
return false;
} if (root1->val != root2->val) {
return false;
} return isIdentical(root1->left, root2->left) && isIdentical(root1->right, root2->right);
} bool hasSubtree(TreeNode *root1, TreeNode *root2, unordered_map<TreeNode *, int> &node_count)
{
if (root1 == nullptr || root2 == nullptr) {
return false;
} if (node_count[root1] < node_count[root2]) {
return false;
} else if (node_count[root1] > node_count[root2]) {
return hasSubtree(root1->left, root2, node_count) || hasSubtree(root1->right, root2, node_count);
} else {
return isIdentical(root1, root2);
}
} void clearTree(TreeNode *&root)
{
if (root == nullptr) {
return;
}
clearTree(root->left);
clearTree(root->right);
delete root;
root = nullptr;
} int main()
{
TreeNode *root1, *root2;
unordered_map<TreeNode *, int> node_count; while (true) {
constructTree(root1);
if (root1 == nullptr) {
break;
}
constructTree(root2);
if (root2 == nullptr) {
break;
} countNode(root1, node_count);
countNode(root2, node_count);
if(hasSubtree(root1, root2, node_count)) {
printf("Yes\n");
} else {
printf("No\n");
} node_count.clear();
clearTree(root1);
clearTree(root2);
} return ;
}

《Cracking the Coding Interview》——第4章:树和图——题目8的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  9. 《Cracking the Coding Interview》——第4章:树和图——题目9

    2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...

随机推荐

  1. mysql : 修改数据库权限

    解决步骤 第一步,点击用户 注意!!! 编辑权限,在我们设置权限之前,我们需要先重新加载才能生效, 如果不用编辑的话,直接按重新载入编辑,这个相当于保存. 中文意思(注意看那段话) 第二步 选择要处理 ...

  2. MySQL数据库实验五:数据更新

    实验五   数据更新 一.实验目的 掌握数据更新操作的用法. 二.实验环境 三.实验示例 1.往基本表SC中插入元组. ①    INSERT INTO S(S#,SNAME,AGE,SEX) VA ...

  3. IOS 自定义代理delegate方法

    创建一个自定义代理 @class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJT ...

  4. BFS变换素数,POJ(3126)

    题目链接:http://poj.org/problem?id=3126 解题报告: #include <iostream> #include <queue> #include ...

  5. vuejs中v-if的深层用法v-else,v-else-if,key

    <div id='root'> <div v-if='show'>helle world</div> <button @click='handleClick' ...

  6. maven项目右键快捷方式,然后点击Run As

    在某一个maven项目右键快捷方式,然后点击Run As就可以发现几个Maven的命令: 1.Maven Build: 这个命令用于编译Maven工程,执行命令后会在target文件夹中的classe ...

  7. linq 和 lmabda 表达式 的用法 和优劣 转自 农码一生

    https://www.cnblogs.com/zhaopei/p/5746414.html

  8. 在IDEA中创建Maven项目和添加tomcat

    IDEA中创建是一种创建maven项目的办法,但不推荐,因为没有使用统一的骨架,可以一开始就选择创建maven项目,步骤如下: 第一步 第二步:设置项目 第三步:进行配置好maven,加上 arche ...

  9. Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70198541 本文出自方志朋的博客 今天一时兴起,想用python爬爬自己的博客,通过数据 ...

  10. Scott Young-《如何高效学习》

    1.如果你只用一种方式了解某样事物,那么你就没有真正了解它.事情真正含义的秘密取决于我们如何将其与我们所了解的其他事情相联系.很好联系的内容可使你将想法融于脑中,从各种角度看问题,直至你找到合适自己的 ...