Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <climits>
- #include <algorithm>
- class Tree {
- private:
- struct TreeNode {
- int val;
- TreeNode *l, *r;
- TreeNode(int val, TreeNode *l = nullptr, TreeNode *r = nullptr) :
- val(val), l(l), r(r) {}
- };
- size_t size;
- TreeNode *root;
- TreeNode *__insert(TreeNode *root, TreeNode *node) {
- if(!root)
- return node;
- if(node->val < root->val)
- root->l = __insert(root->l, node);
- else
- root->r = __insert(root->r, node);
- return root;
- }
- size_t __minimal_height(TreeNode *root, size_t current_height) {
- if(!root->l && !root->r)
- return current_height + 1;
- if(!root->l) return __minimal_height(root->r, current_height + 1);
- if(!root->r) return __minimal_height(root->l, current_height + 1);
- return std::min(__minimal_height(root->l, current_height + 1),
- __minimal_height(root->r, current_height + 1));
- }
- public:
- Tree() : size(0), root(nullptr) {}
- void insert(int val) {
- root = __insert(root, new TreeNode(val));
- }
- size_t minimal_height() {
- return __minimal_height(root, 0);
- };
- };
- int main() {
- Tree t;
- int val;
- while(std::cin >> val) {
- t.insert(val);
- }
- std::cout << t.minimal_height();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement