剑指Offer16 判断子树
/*************************************************************************
> File Name: 17_MirrorOfBinaryTree.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月30日 星期二 17时12分37秒
************************************************************************/ #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <bits/stdc++.h> using namespace std; // 二叉树结构体
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
}; TreeNode* createTree()
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = ;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = ;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = ;
root->right->left = NULL;
root->right->right = NULL;
root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->val = ;
root->left->left->left = NULL;
root->left->left->right = NULL;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->val = ;
root->left->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->left->val = ;
root->left->right->left->left = NULL;
root->left->right->left->right = NULL;
root->left->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->right->val = ;
root->left->right->right->left = NULL;
root->left->right->right->right = NULL; return root;
} TreeNode* createSubTree()
{
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = ;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = ;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = ;
root->left->left = NULL;
root->left->right = NULL;
root->right->left = NULL;
root->right->right = NULL; return root;
} bool DoesTree1HaveTree2(TreeNode* root1, TreeNode* root2)
{
if (root2 == NULL)
return true;
if (root1 == NULL)
return false;
if (root1->val != root2->val)
return false; return DoesTree1HaveTree2(root1->left, root2->left) && DoesTree1HaveTree2(root1->right, root2->right);
} bool hasSubTree(TreeNode* root1, TreeNode* root2)
{
int ret = false;
if (root1!=NULL && root2!=NULL)
{
if (root1->val == root2->val)
ret = DoesTree1HaveTree2(root1, root2);
if (ret == false)
ret = hasSubTree(root1->left, root2);
if (ret == false)
ret = hasSubTree(root1->right, root2);
}
} // 层序遍历二叉树
void PrintTreeByLevel(TreeNode* root)
{
if (root == NULL)
return; vector<TreeNode*> vec;
vec.push_back(root);
int cur = ;
int last = ; while (cur < vec.size())
{
last = vec.size();
while (cur < last)
{
printf("%d ", vec[cur]->val);
if (vec[cur]->left != NULL)
vec.push_back(vec[cur]->left);
if (vec[cur]->right != NULL)
vec.push_back(vec[cur]->right); ++ cur;
}
printf("\n");
}
} int main()
{
TreeNode* tree1 = createTree();
TreeNode* tree2 = createSubTree(); PrintTreeByLevel(tree1);
printf("\n");
PrintTreeByLevel(tree2);
printf("\n"); bool ret = hasSubTree(tree1, tree2);
if (ret == true)
printf("Yes\n");
else
printf("No\n"); return ;
}
剑指Offer16 判断子树的更多相关文章
- 剑指offer 判断树是不是对称的
html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...
- 剑指OFFER18 判断一个二叉树的子树
public class a18_IsSubTree { public static boolean hasSubTree(TreeNode treeRoot1, TreeNode treeRoot2 ...
- 剑指Offer22 判断数组是否为某二叉搜索树的后序遍历
/************************************************************************* > File Name: 22_Sequen ...
- [剑指Offer]判断一棵树为平衡二叉树(递归)
题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 【剑指offer-16】数值的整数次方,C++实现(递归)
原创博文,转载请注明出处! 1.题目 给定一个double类型的浮点数base和int类型的整数exponent,求base的exponent次方.注意:不得使用库函数,同时不需要考虑大数 ...
- 剑指offer--16.数组中重复的数字
时间限制:1秒 空间限制:32768K 热度指数:198342 本题知识点: 数组 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复 ...
- 剑指Offer-16.合并两个排序的链表(C++/Java)
题目: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 分析: 可以用一个新的节点,来去比较两个单调递增的链表当前节点的值,如果p1当前的值小于p2,则新 ...
- 剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。
1 题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 2 思路与方法 迭代法:两个链表中较小的头结点作为合并后头结点,之后依次合并两个链表中较小的 ...
随机推荐
- 跟SAP系统集成的Android应用
首先吐槽一点,这是我的第一个Android应用,很糙. 这个应用适合于上了SAP系统的企业内部使用,并且限于制造型MTO模式,需要针对生产订单报工操作的场景,因为此应用主要的一个目的,就是用来方便报工 ...
- 11.聚合(Aggregation)
聚合关系是关联关系的一种特例,它体现的是整体与部分的关系,即has-a的关系,此时整体与部分之间是可分离的,它们可以具有各自的生命周期.比如计算机与CPU.公司与员工的关系等.表现在代码层面,和关联关 ...
- EXTJS项目实战经验总结一:日期组件的change事件:
1 依据选择的日期,加载相应的列表数据,如图: 开发说明 1 开发思路: 在日期值变化的事件中获得选择后的日期值,传给后台,然后从后台加载相应的数据 2 问题:在查看extjs2.2 的a ...
- 响应式的dribbble作品集魔术布局展示效果
在线演示1 本地下载 相信做设计的朋友肯定都知道dribbble.com,它是一个非常棒的设计师分享作品的网站,全世界数以万计的设计高手和行家都在这个网站上分享自己的作品,当然,如果你常在上面闲逛的话 ...
- Swift学习笔记十
枚举 一个枚举为一组相关联的值定义一个通用类型,并且让你可以在代码中类型安全地操作这些值. C中的枚举将关联的名字指派给一系列整型值.Swift中的枚举类型更为活泼,并不需要为每个成员指定值,如果指定 ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- linux C(hello world)三个数最大和三个数最新
- Apache DbUtils - JDBC轻量级封装的工具包
前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的例如大家常用的 ...
- [Express] Level 1: First Step
Installing Express Let's start building our new Express application by installing Express. Type the ...
- 【React Native 实战】旋转图片验证码
1.前言蘑菇街用打乱方向的图片作为验证码,既起到了验证码的作用又宣传了图片,今天我们就用React Native来实现这样的功能. 2.属性 Image标签属性resizeMode enum('cov ...