1 #include <stdio.h> 2 3 typedef int ElementType; 4 5 struct BinarySearchTreeNode 6 { 7 ElementType Element; 8 struct BinarySearchTreeNode *Left; 9 struct BinarySearchTreeNode *Right; 10 }; 11 12 //if doesn't find,return -1,TreeRoot is 0 13 int BinarySearchTreeNodeFindLevel(struct BinarySearchTreeNode *TreeRoot,struct BinarySearchTreeNode *NodeToBeFind) 14 { 15 if(TreeRoot == NULL || NodeToBeFind == NULL) 16 { 17 return -1; 18 } 19 if(TreeRoot -> Element == NodeToBeFind -> Element) 20 { 21 return 0; 22 } 23 24 int Level = BinarySearchTreeNodeFindLevel(TreeRoot -> Left,NodeToBeFind); 25 if(Level == -1) 26 { 27 Level = BinarySearchTreeNodeFindLevel(TreeRoot -> Right,NodeToBeFind); 28 } 29 if(Level != -1) 30 { 31 return (Level+1); 32 } 33 return -1; 34 } 35 36 int main() 37 { 38 //int LevelNum = BinarySearchTreeNodeFindLevel(TreeRoot,BinarySearchTreeNodeFind(39,TreeRoot)); 39 //printf("%d ",LevelNum); 40 return 0; 41 }