/*************************************** * Sort binary tree, let f = (maximum value + minimum value)/2, design an algorithm to find the node closest to the F value and greater than the F value. Complexity: O (n2) no score *//********************************** **************************************// /query binary trees, the maximum value is the rightmost node, and the minimum value is the leftmost node // obtain F. Find the value near F binarynode * findf (binarynode * root) {If (! Root) return NULL; int maxval = 0; int minval = 0; // maximum binarynode * pnode = root; while (pnode-> m_right) {pnode = pnode-> m_right ;} maxval = pnode-> m_data; // minimum pnode = root; while (pnode-> m_left) pnode = pnode-> m_left; minval = pnode-> m_data; if (minval = maxval) return NULL; int F = (maxval + minval)/2; pnode = root; // unchanged, the query node must be on the pnode subtree // while (pnode-> m_left | pnode-> m_right) while (1) {If (pnode-> m_data <= f) pnode = pnode-> m_right; If (pnode-> m_data> F) {If (pnode-> m_left & pnode-> m_left-> m_data> F) pnode = pnode-> m_left; elsereturn pnode; }}// return pnode;} void testofquerytree () {const int size = 10; int iarr [size]; initarr (iarr, size); showarr (iarr, size); binarynode * root = buildbinarytree (iarr, 0, size-1); cout <findf (Root)-> m_data <Endl ;}