#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
typedef int T;
struct BinaryNode{
T data;
BinaryNode *left, *right;
BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(d), left(l), right(r) {};
};
int height(BinaryNode* root);
Implement the height function for a binary tree:
int height(BinaryNode* root )
my code:
#include"BST.h"
int height(BinaryNode* root ){
int leftheight,rightheight;
if(root==NULL) return -1;
else{
leftheight = height(root->left);
rightheight = height(root->right);
return (leftheight>rightheight)? (leftheight+1) : (rightheight+1);
}
}