树的子结构

简介:

面试题目:输入两颗二叉树A,B,判断B是不是A的子结构;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <stack>
using  namespace  std;
 
typedef  struct  BinaryTreeNode{
     int  value;
     BinaryTreeNode * lchild;
     BinaryTreeNode *rchild;
}BinaryTreeNode;
typedef  BinaryTreeNode * BiTree;
 
void  CreateBiTreeByLevel(BiTree &t, int  Array[], int  i, int  len)
{
     if (Array[i]==0||i > len)  return ;
     t =  new  BinaryTreeNode;
     t->value = Array[i];
     t->lchild = NULL;
     t->rchild = NULL;
     CreateBiTreeByLevel(t->lchild,Array,2*i,len);
     CreateBiTreeByLevel(t->rchild,Array,2*i+1,len);
}
 
void  display(BiTree *t)         //显示树形结构
{
     if (*t!=NULL)
     {
         cout<<(*t)->value;
         if ((*t)->lchild!=NULL)
         {
             cout<< '(' ;
             display(&(*t)->lchild);
         }
         if ((*t)->rchild!=NULL)
         {
             cout<< ',' ;
             display(&(*t)->rchild);
             cout<< ')' ;
         }
     }
}
bool  ifTheSameTree(BiTree &t1,BiTree &t2)
{
     if  (t2 == NULL) return  true ; //B树为空,肯定是相同的树
     if (t1 == NULL) return  false ;
     if  (t1->value!=t2->value) return  false ; //节点不同,不是
     else
     {
         return  ifTheSameTree(t1->lchild,t2->lchild) & ifTheSameTree(t1->rchild,t2->rchild); //左右均为true返回true
     }
}
bool  isSubBinTree(BiTree &t1,BiTree &t2)
{
     bool  result =  false ;
     if (t1 != NULL && t2 != NULL)
     {
         if  (t1->value == t2->value) //根节点相同,就判断A,B树是否一样
         {
             result = ifTheSameTree(t1,t2);
         }
         if (!result)result = isSubBinTree(t1->lchild,t2); //判断左子树
         if  (!result) result = isSubBinTree(t1->rchild,t2); //判断右子树
     }
     return  result;
}
 
int  main()
{
     BiTree A,B;
     //InitBiTree(&T);
     int  a[14] = {0,1,2,3,4,5,6,0,0,0,7,0,8,9}; //从下标1开始,0标识没有节点
     int  b[4] = {0,6,8,9};
     CreateBiTreeByLevel(A,a,1,13);
     CreateBiTreeByLevel(B,b,1,2);
     display(&A);
     display(&B);
     if (isSubBinTree(A,B))cout<< "B is A subTree" ;
     else  cout<< "B is Not A subTree" ;
     system ( "pause" );
}

 

来源:http://blog.csdn.net/xiaobo620/article/details/7957539





本文转自夏雪冬日博客园博客,原文链接:http://www.cnblogs.com/heyonggang/p/3361532.html,如需转载请自行联系原作者

目录
相关文章
|
Linux Apache 弹性计算
|
消息中间件 Cloud Native 中间件
阿里云发布新一代云原生产品,加速企业向现代 IT 架构演进
在 6 月 9 日 2020 阿里云线上峰会上,云原生应用平台产品总监赵林(丹臣)发表了《云原生 2020 新产品发布 传统应用架构往现代应用架构快速演进的基础设施》的主题演讲,详细介绍了阿里云全新发布的容器、中间件、Serverless 等产品。随着数字经济的快速发展和扩张,越来越多的企业开始采用云原生计算的思想和技术,以主导企业的数字化转型架构。
1447 83
阿里云发布新一代云原生产品,加速企业向现代 IT 架构演进
|
JavaScript
JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
JS数组排序技巧汇总(冒泡、sort、快速、希尔等排序)
157 0
|
关系型数据库 MySQL 开发工具
在wsl中部署.netcore应用
在wsl中部署.netcore应用
|
Web App开发 编解码 移动开发
2023年最新前端面试题汇总大全(含答案超详细,HTML,JS,CSS汇总篇)-- 持续更新 5
2023年最新前端面试题汇总大全(含答案超详细,HTML,JS,CSS汇总篇)-- 持续更新
508 0
|
JavaScript 网络安全 开发工具
基于OSS搭建云上个人博客-1
基于OSS搭建云上个人博客-1
254 0
基于OSS搭建云上个人博客-1
通过案例带你轻松玩转JMeter连载(57)
通过案例带你轻松玩转JMeter连载(57)
192 0
通过案例带你轻松玩转JMeter连载(57)
|
安全 Windows
Adobe 和微软通过微软边缘为 1 亿 Windows 用户带来行业领先的 Acrobat PDF 体验
Adobe 和微软通过微软边缘为 1 亿 Windows 用户带来行业领先的 Acrobat PDF 体验。
Adobe 和微软通过微软边缘为 1 亿 Windows 用户带来行业领先的 Acrobat PDF 体验
|
JavaScript
js 选取table中checkbox选中行的某一列
js 选取table中checkbox选中行的某一列
314 0
|
iOS开发
iOS MachineLearning 系列(6)—— 视频中的物体轨迹分析
轨迹分析是比物体追踪更上层的一种应用。Vision框架中提供了检测视频中多个物体的运动轨迹等能力,在健身,体育类应用中非常有用。
305 0