➤ Problem Link : NICEBTRE
👉 Hint : Construct and then find height of binary tree
✅ C++ Solution :
#include<bits/stdc++.h> using namespace std; struct btnode { char info,count; struct btnode *lchild,*rchild; }; typedef struct btnode BTNODE; int i=0; BTNODE *insert(string s) { BTNODE *r=(BTNODE *)malloc(sizeof(BTNODE)); r->info=s[i]; r->lchild=NULL; r->rchild=NULL; if(s[i++]=='n') { r->lchild=insert(s); r->rchild=insert(s); } return r; } int height(BTNODE *root) { if(root->lchild==NULL && root->rchild==NULL) return 0; return 1+max(height(root->lchild),height(root->rchild)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) { BTNODE *root=NULL; string s; cin>>s; root=insert(s); cout<<height(root)<<endl; i=0; } }
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!