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. Jmeter入门5 关联 响应数据传递-正则表达式提取器

    在测试过程中,遇到一个问题:用户登录成功后服务器会返回一个登录凭证,之后所有的操作都需要带上此凭证.我们怎么获取登录凭证并传递给后续的操作? Jmeter提供了正则表达式提取器,用变量提取参数,后续通 ...

  2. Jenkins添加项目说明,增加项目描述

    背景:往往正常Jenkins上呈现的内容,太过简短,不易直观看了解项目是干嘛的,如下面的内容: 解决方案,使用插件,Extra Columns Plugin 安装成功后配置,需要结合自定义视图使用,新 ...

  3. POJ-1509 Glass Beads---最小表示法模板

    题目链接: https://vjudge.net/problem/POJ-1509 题目大意: 给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小. 解题思路: 最小表示法模板 注 ...

  4. Jmeter文件目录,功能简介

    1.Jmeter文件目录:1)bin文件: Jmeter启动:bin/jmeter.bat Jmeter日志文件:jmeter.log Linux的启动文件:Jmeter.sh Jmeter系统配置文 ...

  5. 2017.9.24 基于HTML+JavaScript+CSS的开发案例&&JavaScript+CSS+DIV实现表格变色

    1.JavaScript+CSS+DIV实现下拉菜单 1.1 层标签<div> 基本语法: <div id="层编号" style="position: ...

  6. =>符号的意义

    => 是 Oracle 中调用存储过程的时候, 指定参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用. 下面是具体的例子. 参数的默认值SQL> CREATE ...

  7. node-inspector调试工具使用方法

    开发node.js程序使用的是javascript语言,其中最麻烦的还是调试,这里介绍一下node-inspector使用方法.具体资料可以看参考资料中的GITHUB文档. 工具/原料   node. ...

  8. 深入浅出:promise的各种用法

    https://mp.weixin.qq.com/s?__biz=MzAwNTAzMjcxNg==&mid=2651425195&idx=1&sn=eed6bea35323c7 ...

  9. Lucene实战

    导包

  10. SqlServer2008/2005数据库日志收缩

    1.SQL2008数据库USE [master]GOALTER DATABASE 数据库名称 SET RECOVERY SIMPLE WITH NO_WAITALTER DATABASE 数据库名称 ...