-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathisBST.cpp
61 lines (48 loc) · 1.34 KB
/
isBST.cpp
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
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
struct Node{
int key;
Node *left,*right;
Node (int k){
key=k;
left=right=NULL;
};
};
bool areIdentical(Node *T, Node *S){
if(T==NULL && S==NULL){
return true;
}
if(T==NULL||S==NULL){
return false;
}
return (T->key==S->key && (areIdentical(T->left,S->left)) && (areIdentical(T->right,S->right)));
}
bool isSubtree(Node *T, Node *S){
if(S==NULL) return true;
if(T==NULL) return false;
if(areIdentical(T,S)) return true;
return (isSubtree(T->left, S)||isSubtree(T->right,S));
}
int main()
{
Node *T=new Node(26);
T->left=new Node(10);
T->right=new Node(3);
T->left->left=new Node(4);
T->left->right=new Node(3);
T->left->left->right=new Node(6);
Node *S=new Node(10);
S->left=new Node(4);
S->right=new Node(3);
S->left->right=new Node(6);
if(isSubtree(T,S))
cout<<"yes";
else
cout<<"no";
return 0;
}