《Cracking the Coding Interview》——第4章:树和图——题目8
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
		
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
 - 《Cracking the Coding Interview》读书笔记
		
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
 - Cracking the coding interview
		
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
 - Cracking the Coding Interview(Trees and Graphs)
		
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
 - Cracking the coding interview目录及资料收集
		
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
 - 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 ...
 - 二刷Cracking the Coding Interview(CC150第五版)
		
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
 - Cracking the Coding Interview 150题(二)
		
3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...
 - 《Cracking the Coding Interview》——第4章:树和图——题目9
		
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
 
随机推荐
- EF写INNER JOIN 链接
			
面对多表的查询,一般都是多表连接后下面再写条件,但是有一种写法可以提升一下EF生成的语句的效率 首先先去查询每一个表,把每一个表对应的条件附加上去,注意:过滤数据最多的条件放在首先位置 var lt ...
 - 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)
			
点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...
 - IIS/IIS Express中遇到的证书问题
			
上面这幅图大家应该不陌生(觉得陌生的话就不用看下面的内容了,呵呵),再放上中英两段关键字: 根据验证过程,远程证书无效. The remote certificate is invalid accor ...
 - xrdp 安装后 WINDOWS远程登录出错
			
xrdp需要vnc作为基础服务, sudo apt-get install tightvncserver 树莓派上这个命令运行下再连就好了
 - C# unchecked运算符
			
一.C# unchecked运算符 unchecked运算符用于取消整型算术运算和转换的溢出检查. 二.提示 默认情况下,都是unchecked选项.因此,只有在需要把几个未检查的代码行放在一个明确标 ...
 - javascript入门笔记3-dom
			
1.通过ID获取元素 document.getElementById("id") <!DOCTYPE HTML> <html> <head> & ...
 - C++声明之CV限定符
			
目录 1.const 1.1 const obj 如果调用 non-const member fun会编译出错 经典错误 1.2 例子:STD里的操作符重载 1.3 例子:<cpp primer ...
 - vc导出调用dll的两种方式
			
一.stdcall 1. #define DLLEXPORT _declspec(dllexport) _stdcall, int DLLEXPORT func(const char *peer,u ...
 - 转:mysql远程连接 Host * is not allowed to connect to this MySQL server
			
在本机登入mysql后,更改"mysql"数据库里的"user"表里的"host"项,从"localhost"改为'%' ...
 - linux下 利用 rz 命令上传文件
			
1. 如何安装? 1)编译安装 root 账号登陆后,依次执行以下命令: # cd /tmp # wget http://www.ohse.de/uwe/releases/lrzsz-0.12.20 ...