-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
48 lines (40 loc) · 951 Bytes
/
Copy pathBST.cpp
File metadata and controls
48 lines (40 loc) · 951 Bytes
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
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int x) {
data = x;
left = right = nullptr;
}
};
class BST {
public:
Node* root;
BST() {
root = nullptr;
}
Node* insert(Node* node, int x) {
if (!node) return new Node(x);
if (x < node->data)
node->left = insert(node->left, x);
else
node->right = insert(node->right, x);
return node;
}
bool search(Node* node, int x) {
if (!node) return false;
if (node->data == x) return true;
if (x < node->data)
return search(node->left, x);
return search(node->right, x);
}
void inorder(Node* node) {
if (!node) return;
inorder(node->left);
cout << node->data << " ";
inorder(node->right);
}
};