Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- struct Ttree
- {
- int val;
- Ttree *left, *right;
- };
- struct Ttree *addNode(int x, Ttree *tree)
- {
- if (tree == NULL)
- {
- tree = new Ttree;
- tree->val = x;
- tree->left = NULL;
- tree->right = NULL;
- }
- else
- {
- if (x < tree->val)
- tree->left = addNode(x, tree->left);
- else
- tree->right = addNode(x, tree->right);
- }
- return tree;
- }
- void printLeafs(Ttree *root)
- {
- if (root)
- {
- printLeafs(root->left);
- if (!root->left && !root->right)
- cout << root->val << endl;
- printLeafs(root->right);
- }
- }
- void print_Tree(Ttree *p, int level)
- {
- if (p)
- {
- print_Tree(p->right, level + 1);
- for (int i = 0; i < level; i++) cout << " ";
- cout << p->val ;
- cout << endl;
- print_Tree(p->left, level + 1);
- }
- }
- int dep_count=0;
- int max(int x, int y)
- {
- if (x > y)
- {
- return x;
- }
- }
- int Depth(Ttree *Ptr, int dep_count)
- {
- if (Ptr == NULL)
- return dep_count;
- return max(Depth(Ptr->left, dep_count + 1), Depth(Ptr->right, dep_count + 1));
- }
- int main()
- {
- int num = 0;
- cin >> num;
- Ttree *root = NULL;
- while (num != 0)
- {
- root=addNode(num,root);
- cin >> num;
- }
- printLeafs(root);
- print_Tree(root, Depth(root, dep_count));
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement