From http://blog.csdn.net/mishifangxiangdefeng/article/details/7983809
# Include <iostream> # include <time. h> using namespace STD; // The result of the first request for the minimum value represents the struct node {int key; node * Next; // points to the next element node * P in the same layer; // parent pointer node * left; // left child node * right; // right child node (int K): Key (K), next (null ), P (null), left (null), right (null) {}}; // calculate the second small value node * find_s2 (node * head) {node * P, * Q, * r, * t; // Step 1: Calculate the minimum value. // compare the two values. A smaller value enters the next round, this loop ends when only one element is left! = NULL) {// starting from the first element, head points to the first p = head; head = NULL; while (P) {// P compared with p-> next, smaller elements are promoted to If (p-> next! = NULL) {q = p-> next; r = new node (min (P-> key, Q-> key); R-> left = P; r-> right = Q; P-> P = r; q-> P = r; P = Q-> next;} // if this group of data has an odd number, the last element is directly promoted to else {r = new node (p-> key); R-> left = P; P-> P = r; P = p-> next ;} // head points to the first one in the group of data that is smaller than the end. t is used to link the head to the chain table if (Head = NULL) {head = R; T = head;} else {T-> next = r; t = r ;}}return head;} int ws_ws (node * head) {// step2: the second smallest value // Min is used to store the minimum value, and min2 is used to store the second smallest value int min = head-> key, min2 = 0x7fffffff; // compare node * P = head down from the root node; // compare to the end point of the leaf when the loop ends while (p-> left! = NULL) {// the value of the current node is from the right child if (p-> Right & P-> right-> key = min) {min2 = min (min2, p-> left-> key); P = p-> right ;} // The value of the current node is from the left child else {// If (p-> right) min2 = min (min2, p-> right-> key); P = p-> left;} return min2;} // test int main () {int A [8] = {0 }; srand (INT) time (0); node * head = NULL; // generate 8 random test data for (INT I = 0; I <8; I ++) {A [I] = rand () % 100; // construct the bottom node of the tree * P = new node (A [I]); P-> next = head; head = P ;}for (INT I = 0; I <8; I ++) cout <A [I] <''; // run the algorithm and output the result head = find_s2 (head); int ret = ws_ws (head); cout <Endl <RET <Endl; return 0 ;}