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. Windows Thin PC(7月2日发布)下载+激活+汉化

    在7月2日Windows 7 瘦身版Windows Thin PC(WinTPC)完成了RTM版的编译开发,WinTPC是一个面向企业用户的产品,主要面向虚   拟桌面基础架构(VDI)消费者,Win ...

  2. css隐藏元素

    在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应点击.下面一个个列出,选一个适合你的 { display: none; /* 不 ...

  3. phpMyAdmin提示找不到mcrypt和mbstring模块

    yum install php-mcryptyum install php-mbstringphp -m 查看是否安装成功 service httpd restart 重启服务器 注: 这里可能会出现 ...

  4. 创建git及其初始化

    创建一个版本库非常简单,首先,选择一个合适的地方,创建一个空目录: $ mkdir learngit $ cd learngit $ pwd /Users/michael/learngit 初始化一个 ...

  5. SQL之Case when 语句

    --case简单函数 (把多列变成单列) ' then '女' when ' then '男' else '其他' end from [Northwind].[dbo].[Users] --case搜 ...

  6. Win10预览版激活信息

    微软在10月2日零点正式公开了Win10预览版的下载地址,这个时间大家应该逐步开始安装工作了,因此提出下面两个问题的用户特别多,IT之家再稍作告知一下.1.Win10预览版安装密钥是什么?答:NKJF ...

  7. 深入理解JVM类加载机制 classloader

    转自https://www.cnblogs.com/ygj0930/p/6536048.html

  8. 判断一个Object是否为数组Array的方法

    1.constructor constructor 属性是每个有原型对象的原型成员 arr.constructor == Array  //true说明arr是数组 2.isArray arr.isA ...

  9. 牛客小白月赛1 I あなたの蛙が帰っています 【卡特兰数】

    链接:https://www.nowcoder.com/acm/contest/85/I题目描述 あなたの蛙が帰っています!  蛙蛙完成了一趟旅行,回家啦!但它还是没有去它心中非常想去的几个地方.总共 ...

  10. Java - 关于基础数据类型的形参和返回值

    1. 当基础数据类型被当作形参时,最好使用其包装类,因为这样可方便调用者传参(基础数据类型亦或是其包装类都可)   2. 当基础数据类型被当作返回值时,最好使用原型,因为这样可以方便调用者接收返回值( ...