Using Iteration
int height(Node* node) {
// code here
int count = -1;
if(node == NULL) return count;
queue<Node*> q;
q.push(node);
while(!q.empty()) {
int len = q.size();
for(int i=0; i<len; i++) {
Node *cur = q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
count++;
}
return count;
}
Using Recursion
int height(Node* node) {
// code here
if(node == NULL) return -1;
int lh = height(node->left);
int rh = height(node->right);
return max(lh, rh) +1;
}