What is the rule of the path in the binary lookup tree?
Let's analyze it once.
Input1 4 2 3At the outset, we define a range that can accommodate [0, INFINITE)//Infinite means infinityread in 1 First, then read into the first number is 1,4>1>0, the range narrows to [1, INFINITE) The previous number that was read into 2,2 was 4,2<4<infinite, so the interval range is reduced to [1,4] again read into the 3,3 the previous number is 2,3>2>1, so the interval range is reduced to [2,4] There are no numbers behind, output yesinput:1 4 2 5 3read in 1, read in 4,4>1>0Interval1, INFINITE) read in 2,2<4<infinite, Interval [1,4] read in 5,5>2>1Interval2,4] !but 5 no longer belongs to this range! After the input is discarded, output no
Wit a little, the beginning of the interval to take [1,max] is ok = =
Done!
A variety of boundary conditions also to test good OH =
#include <iostream>using namespacestd;intMain () {intt, N, Min, Max, Oldnum, Newnum; /*t Number of tests n number of each time * min current interval min max current interval Max * Oldnum previous one obtained number Newnum*/Cin>> T >>N; for(intj =0; j<t; J + +) {min=1, max =N; CIN>>Oldnum; if(n = =1) {cout<<"YES"<<Endl; Continue; } for(inti =0; I < n-1; i++) {cin>>Newnum; if(Newnum>oldnum && oldnum>min) {min=Oldnum; } Else if(Newnum < Oldnum && Oldnum <max) {Max=Oldnum; } oldnum=Newnum; if(min <= newnum && newnum <=max) { if(i = = N-2) {cout<<"YES"<<Endl; } Continue; } Else{cout<<"NO"<<Endl; while(GetChar ()! ='\ n'); Break; } } }}
Search in a Binary search Tree (25)