We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
openSystemlettestBSTinput=letrectestinputstackroot=matchinput,stackwith//whenever current number is smalller than root return false|(h::t),_whenh<root->false//when top of stack is smaller then current number//make the top of stack the new root and pop the stack|(h::t),(x::s)whenx<h->testinputsx//if input is not empty and other states are not matched//push current number into stack|(h::t),_->testt(h::stack)root//if all numbers a processed//means input is valid pre-order sequence of BST return true|[],_->true//if anything else happen throw an error|_->failwith"something goes wrong"testinput[]-1[<EntryPoint>]letmainargv=letnum=Console.ReadLine()|>intletinput=[foriin1..numdo//no need to know how many nodes in the tree so ignore itConsole.ReadLine()|>ignorelettemp=Console.ReadLine()yieldtemp.Split(" ")|>List.ofArray|>List.map(funx->x|>int)]foriininputdoif(testBSTi)thenprintfn"%s""YES"elseprintfn"%s""NO"0// return an integer exit code
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Valid BST
You are viewing a single comment's thread. Return to all comments →
F# solution with comment: