Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Node {
- Node* l;
- Node* r;
- int val;
- Node(int val) :val(val), l(nullptr), r(nullptr){}
- };
- Node* root = nullptr;
- void add(int a, Node* cur) {
- if (cur == nullptr) {
- root = new Node(a);
- return;
- }
- if (a < cur->val) {
- if (cur->l == nullptr) {
- cur->l = new Node(a);
- return;
- }
- add(a, cur->l);
- }
- else if (a > cur->val) {
- if (cur->r == nullptr) {
- cur->r = new Node(a);
- return;
- }
- add(a, cur->r);
- }
- }
- int count(Node* cur) {
- if (cur == nullptr) return 0;
- return count(cur->l) + count(cur->r) + 1;
- }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int a;
- fin >> a;
- while (a != 0) {
- add(a, root);
- fin >> a;
- }
- fout << count(root) << endl;
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment