650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/85/AD/wKiom1esENLzdGt4AACY_uRxu7s509.png "title=" capture. PNG "alt=" Wkiom1esenlzdgt4aacy_urxu7s509.png "/>
Data structure of #include <iostream> #include <vector>//nodes struct node{int _data;int _ Weight node* _left; node* _right; Node (const int& x = 0,int weight=0): _left (null), _right (null), _data (x) , _weight (weight) {}};//Node* createtree (const int* array,const int* weight, Size_t size,int& i)//i=0{/*if (array == null | | weight==null | | size == 0) return null;*/if (array[i] == ' # ') return null; Node* root = new node (Array[i],weight[i]); Root->_left = createtree (Array, Weight,size,++i); Root->_right = createtree (array, weight, size, ++i);return Root;} The first sequence traversal, the node is placed in the container V, the corresponding depth of the junction is placed in the Vdepth (root node depth is 0) node* pushdata (node* root, int& max, Int& min, std::vector<node*>& v, std::vector<int>& vdepth,int depth) {node* cur = root;if (cur) {v.push_back (cur); Vdepth.push_back (depth );} while (cur) {pushdata (cur->_left, max, min, v, vdepth, ++depth);if (cur- >_left != null) {v.push_back (cur); Vdepth.push_back (--depth);} Else--depth;pushdata (cur->_right, max, min, v, vdepth, ++depth);if (cur->_ Right != null) {v.push_back (cur); Vdepth.push_back (--depth);} else{--depth;if (Cur->_weight > max) max = cur->_weight;if (cur->_ weight < min) Min = cur->_weight;} Return cur;} Return null;} Int bigandsmallweightdistance (NODE*&NBSP;ROOT,&NBSP;INT&&NBSP;MAX,&NBSP;INT&&NBSP;MIN,&NBSP;STD:: vector<node*>& v, std::vector<int>& vdepth, int depth) {PushData ( root, max, min, v, vdepth, depth); int maxindex, minindex;//find the largest and smallest leaf knotThe point in V corresponds to the subscript for (Int i = 0; i < v.size (); ++i) {if (v[i]->_ Weight == max) {maxindex = i;} if (v[i]->_weight == min) {minindex = i;}} int i, j;if (Maxindex > minindex) {i = minindex;j = maxindex;} int retminindex = i;for (i; i <= j; ++i) {if (vdepth[i] < retminindex) {Retminindex = vdepth[i];//retminindex The corresponding V node in the subscript is the common parent of the leaf node you are looking for}}//to this point, We found these two weights the largest and smallest leaf nodes and their common ancestor nodes subscript return vdepth[maxindex] + vdepth[minindex] - 2 * vdepth[retminindex];} Int main () {/*int array[] = { 1, 2, 3, ' # ', ' # ', 4, 5, ' # ', ' # ', 6, ' # ', ' # ', 7, ' # ', 8, ' # ', ' # ' };int weight[] = { 4, 2, 1, ' # ', ' # ', 5, 7, ' # ', ' # ', &Nbsp;8, ' # ', ' # ', 6, ' # ', 3, ' # ', ' # ', };*/int array[] = { 1, 2, 3, 11, ' # ', 12, ' # ', ' # ', ' # ', 4, 5, ' # ', ' # ', 6, 9, ' # ', 10, ' # ', ' # ', ' # ', 7, ' # ', 8 , ' # ', ' # ' };int weight[] = { 4, 2, 2, 13, ' # ', 1, ' # ', ' # ', ' # ', 5, 7, ' # ', ' # ', 8, 9, ' # ', 10, ' # ' , ' # ', ' # ',6, ' # ', 3, ' # ', ' # ', };int i = 0; Node* root = createtree (array,weight,sizeof (array)/sizeof (array[0]), i);std::vector<node*> v;std::vector<int> vdepth;int depth = 0;int maxweight=-1;int Minweight = 100;int distance = bigandsmallweightdistance (root, maxweight, Minweight, v, vdepth, depth); Std::cout << distance << std::endl;system ("Pause");}
This article from "Zero Egg" blog, declined reprint!
Find the distance between the maximum and minimal leaf nodes in a binary tree