-
Notifications
You must be signed in to change notification settings - Fork 0
/
biSearchTree.cpp
69 lines (61 loc) · 1.1 KB
/
biSearchTree.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
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<stdio.h>
using namespace std;
typedef struct Node{
struct Node *l;
struct Node *r;
int val;
}node, *nodep;
int l1,l2;
int n1=0,n2=0;
nodep insert(nodep root, int val){
if(root==NULL){
root = new Node();
root->l=NULL;
root->r=NULL;
root->val=val;
}
else if(val <= root->val){
root->l = insert(root->l, val);
}
else
{
root->r = insert(root->r, val);
}
return root;
}
int getHight(nodep root){
if(root==NULL){
return 0;
}
int l=getHight(root->l);
int r= getHight(root->r);
return max(l, r) + 1;
}
void walk(nodep root, int depth){
if(root==NULL){
return;
}
if(depth==l1){
n1++;
}else if(depth==l2){
n2++;
}
walk(root->l, depth+1);
walk(root->r, depth+1);
}
int main(){
int n;
nodep root=NULL;
cin>>n;
for(int i=0;i<n;i++){
int v;
cin>>v;
root = insert(root, v);
}
int h = getHight(root);
l1=h;
l2=h-1;
walk(root, 1);
printf("%d + %d = %d", n1, n2, n1+n2);
}