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. TP5.1:连接数据库(全局配置、动态配置、DSN配置)

    前提: (1)在app\index\controller文件下新建一个名为Connect.php的控制器文件 (2)建立一个名为user_curd数据库,里面有一张user表,表内容为: 通过全局配置 ...

  2. 寄生构造函数模式 js

    有一点需要说明:首先返回的对象与构造函数或者构造函数的原型属性之间没有关系,也就是说构造函数返回的对象与在构造函数外部创建的对象没有什么不同,为此不能依赖 instanceof 操作符来确定对象类型. ...

  3. vue 中$index $key 已经移除了

    https://cn.vuejs.org/v2/guide/migration.html#index-and-key-移除 之前可以这样: 1 2 3 4 5 6 <ul id="ex ...

  4. MCV 的几种表单提交方式

    一,MVC  HtmlHelper方法 Html.BeginForm(actionName,controllerName,method,htmlAttributes){}   其中actionName ...

  5. scrapy install

    csf@ubuntu:~$ sudo apt install python-scrapy

  6. Spring注解@Value数值取值转换字符串失败

    配置文件(yml)中,配置项如下: cebconfig: INST_CODE: 08801001 SFT_NOTIFY_CEB_CHANNEL: 123456 期望INST_CODE: 0880100 ...

  7. 在c#中using和new这两个关键字有什么意义?

    在c#中using和new这两个关键字有什么意义?答:using 引入名称空间或者使用非托管资源, new 新建实例或者隐藏基类方法

  8. 【Java】基础:常见修饰符(权限修饰符以及abstract、static、final等)与变量的描述

    1. 修饰符 public.protected.private.default abstract.static.final. abstract:抽象类.抽象方法 static:静态变量.静态方法.静态 ...

  9. JavaWeb各大组件生命周期

    JavaWeb各大组件生命周期 servlet生命周期 服务器打开:在第一次请求时实例化与初始化:然后进行服务:最后服务器关闭销毁 Cookie生命周期:存储在客户端 如果不设置过期时间,则表示这个c ...

  10. cordova创建工程添加插件

    创建工程 phonegap创建工程 代码 用以创建自己需要的  工程名   ; 报名  ;类名 ; 应用名 cordova create hello com.example.hello HelloWo ...