//finds the distance to the farthest leaf int BST::nodesOnMaxPathToLeaf(TreeNode* t) { TreeNode* currL = t; TreeNode* currR = t; int x,y;x = 0; y = 0; if(size() == 0) { return -1; } if(t->leftChildPtr != NULL) { currL = currL->leftChildPtr; x = 1 + nodesOnMaxPathToLeaf(currL); } if(t->rightChildPtr != NULL) { currR = currR->rightChildPtr; y = 1 + nodesOnMaxPathToLeaf(currR); } if(x >= y) { return x; } if(y > x) { return y; } }
//finds the distance to the farthest leaf int BST::nodesOnMaxPathToLeaf(TreeNode* t) const { //TODO: if(t==NULL){ return 0; } else if(t->leftChildPtr==NULL && t->rightChildPtr==NULL){ return 1; } else{ if(nodesOnMaxPathToLeaf(t->leftChildPtr)>nodesOnMaxPathToLeaf(t->rightChildPtr)){ return nodesOnMaxPathToLeaf(t->leftChildPtr)+1; } else{ return nodesOnMaxPathToLeaf(t->rightChildPtr)+1; } } }
//calls nodesOnMaxPathToLeaf finds the height of the tree int BST::height() const { //TODO: Must Implement return nodesOnMaxPathToLeaf(root)-1; }
//calls nodesOnMaxPathToLeaf finds the height of the tree int BST::height() { nodesOnMaxPathToLeaf(root); }