//
// main.cpp
// Height of Binary Tree
//
// Created by Himanshu on 11/09/21.
//
#include <iostream>
#include <queue>
using namespace std;
struct node {
int info = 0;
struct node *left, *right;
};
typedef struct node Node;
node* newNode(int data)
{
node* Node = new node();
Node->info = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
int maxHeight (Node *root) {
if (!root) {
return -1;
}
int leftHt = maxHeight(root->left);
int rightHt = maxHeight(root->right);
if (leftHt > rightHt) {
return (leftHt + 1);
} else {
return (rightHt + 1);
}
}
int main() {
Node *root = newNode(1);
root->left = newNode(20);
root->right = newNode(21);
root->right->left = newNode(30);
root->right->right = newNode(31);
cout<<"The height of given tree is: "<<maxHeight(root)<<endl;
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBIZWlnaHQgb2YgQmluYXJ5IFRyZWUKLy8KLy8gIENyZWF0ZWQgYnkgSGltYW5zaHUgb24gMTEvMDkvMjEuCi8vCgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxxdWV1ZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBub2RlIHsKICAgIGludCBpbmZvID0gMDsKICAgIHN0cnVjdCBub2RlICpsZWZ0LCAqcmlnaHQ7Cn07CnR5cGVkZWYgc3RydWN0IG5vZGUgTm9kZTsKCm5vZGUqIG5ld05vZGUoaW50IGRhdGEpCnsKICAgIG5vZGUqIE5vZGUgPSBuZXcgbm9kZSgpOwogICAgTm9kZS0+aW5mbyA9IGRhdGE7CiAgICBOb2RlLT5sZWZ0ID0gTlVMTDsKICAgIE5vZGUtPnJpZ2h0ID0gTlVMTDsKIAogICAgcmV0dXJuKE5vZGUpOwp9CgoKaW50IG1heEhlaWdodCAoTm9kZSAqcm9vdCkgewogICAgaWYgKCFyb290KSB7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgaW50IGxlZnRIdCA9IG1heEhlaWdodChyb290LT5sZWZ0KTsKICAgIGludCByaWdodEh0ID0gbWF4SGVpZ2h0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