A binary tree is a rooted tree whose every node has at most two children.

A labelled binary tree is a binary tree whose every node is labelled with a positive integer; moreover, all labels are distinct.

A BST (binary search tree) is a labelled binary tree in which the label of each node is greater than the labels of all the nodes in its left subtree, and smaller than the labels of all the nodes in its right subtree. For instance, the following is a BST:


The pre-order traversal of a labelled binary tree is defined by the following pseudo-code.

function preorder(node)
    if node is null then

See the following image to get a better intuition:

Pre-order traversal of a BT

The vertices of this binary tree are printed in the following order:

F, B, A, D, C, E, G, I, H

You can read more about BSTs here, and more about pre-order traversal here.


Given a list of integers \$a\$, your task is to determine whether there is a BST whose pre-order traversal prints exactly \$a\$.


  • A non-empty list of distinct positive integers \$a\$.
  • Optionally, the length of \$a\$.


  • A truthy value if \$a\$ is the pre-order traversal of some BST.
  • A falsey value otherwise.


  • Standard rules for valid submissions, I/O, loopholes apply.
  • This is , so shortest solution (in bytes) wins. As usual, don't let ridiculously short solutions in golfy languages discourage you from posting a longer answer in your language of choice.
  • This is not a rule, but your answer will be better received if it includes a link to test the solution and an explanation of how it works.


Input                   ---->   Output

[1]                     ---->   True
[1,2,3,4]               ---->   True
[5,1,4,2,3]             ---->   True
[5,4,3,2,1,6,7,8,9]     ---->   True
[4,2,1,3,6,5,7]         ---->   True
[8,3,1,6,4,7,10,14,13]  ---->   True
[2,3,1]                 ---->   False
[6,3,2,4,5,1,8,7,9]     ---->   False
[1,2,3,4,5,7,8,6]       ---->   False
[3,1,4,2]               ---->   False

Check out this link (courtesy of Kevin Cruijssen) to have a visual look at the examples.


13 Answers 13


JavaScript (Node.js), 49 bytes


Try it online!

Using the fact that for array \$a_0 ... a_{n-1}\$, \$a\$ is the pre-order traversal of some BST iff \$ \forall 0\le i<j<n; a_i<a_{j-1} \implies a_i<a_j \$ holds.

Thanks to Arnauld, save 1 byte.


Jelly, 7 bytes


Try it online!

Returns [4] for traversals, otherwise [].

Essentially uses tsh's algorithm: the "disqualifying" condition for a pre-order traversal is a subsequence of 3 elements that looks like [mid, high, low]. (For example, [20, 30, 10].)

We equivalently check for any subsequences of any length that have index 4 in their permutation lists, which are all lists sorted like [a1 … ak c d b] where the ai are sorted and ai < b < c < d. (Each such list is disqualifying if we look at the last three elements, and each disqualifying list is obviously of this form.)

ŒP          All subsequences.
  Œ¿€       Permutation index of each.
     4ḟ     Set difference of {4} and this list.


A pre-order traversal contains no disqualifying subsequences

Base case: traversal(•) is the empty list. ✓

Induction: traversal(t) is: t.root ++ traversal(t.left) ++ traversal(t.right).

Let [a, b, c] be a subsequence hereof. We'll show c < a < b is impossible.

  • If t.root = a, then c < a < b requires c ∈ t.left and b ∈ t.right, so [a, b, c] is the wrong order.
  • If a, b, c ∈ t.left or a, b, c ∈ t.right, use the induction hypothesis.
  • If a ∈ t.left and c ∈ t.right then c > a.

A list of distinct integers without disqualifying subsequences is the pre-order traversal of a BST

If the list is empty, it's the traversal of the trivial BST •.

If the list is head followed by tail:

  • Let less be the longest prefix of tail of elements less than head, and let more be the rest of the list.
  • Then more[1] > head, and all other more[i] are greater than head too (otherwise [head, more[1], more[i]] would be a disqualifying subsequence).
  • Recurse: turn less and more into BSTs.
  • Now our list is the traversal of

                    /    \
             BST(less)   BST(more),

    and this tree is a valid BST.

  • 1
    \$\begingroup\$ Nice proof. Actually I didn’t proved the formula when I post the answer. I just felt that it is correct after some attempts to construct the BST from input. \$\endgroup\$ – tsh Oct 20 '18 at 3:27

Java 10, 94 bytes

a->{var r=0>1;for(int j=a.length-1,i;j-->0;)for(i=0;i<j;)r|=a[j]>a[i]&a[j+1]<a[i++];return!r;}

Port of @tsh' JavaScript answer.

Try it online.


a->{                      // Method with integer-array parameter and boolean return-type
  var r=0>1;              //  Result-boolean, starting at false
  for(int j=a.length-1,i;j-->0;)
                          //  Loop `j` in the range (length-1, 0]:
    for(i=0;i<j;)         //   Inner loop `i` in the range [0, j):
      r|=                 //    If any are true, change the result to true as well:
         a[j]>a[i]        //     The `j`'th item is larger than the `i`'th item
         &a[j+1]<a[i++];  //     And the `j+1`'th item is smaller than the `i`'th item
  return!r;}              //  After the nested loop, check if the boolean is still false
  • 1
    \$\begingroup\$ TIL that Java booleans can be reassigned with |=. I assume &= would also work? \$\endgroup\$ – J. Sallé Oct 19 '18 at 13:14
  • \$\begingroup\$ @J.Sallé Yep, both |= and &= work as shortcuts for b = b | condition and b = b & condition (where the & and | are shortcuts for && and || in most cases of course). \$\endgroup\$ – Kevin Cruijssen Oct 19 '18 at 13:33

Ruby, 46 40 38 bytes


Try it online!

This works by recursively taking the first element a as a pivot, and checking if the rest of the array can be split in two (using intersection and union: first remove all elements >a, then add them again on the right and check if something has changed).


Retina 0.8.2, 31 bytes


Try it online! Link includes test cases. Uses @tsh's algorithm. Explanation:


Convert to unary.


Find numbers that lie between two subsequent consecutive descending numbers.


Check that the number of matches is zero.


Perl 6, 38 bytes

!*.combinations(3).grep:{[>] .[1,0,2]}

Try it online!


 *.combinations(3)  # All combinations of 3 elements a,b,c
!                 .grep:{            }  # Return false if check succeeds for any combination
                         [>] .[1,0,2]   # Check whether b>a>c, that is b>a and c<a

Haskell, 50 bytes

f(x:r)|(a,b)<-span(<x)r=all(>x)b&&f a&&f b

Try it online!


Scala (68 67 Bytes)

def%(i:Seq[Int])= !i.combinations(3).exists(c=>c(0)<c(1)&c(0)>c(2))

Try it online

Port of @nwellnhof's answer.

Scala (122 103 bytes)

def f(i:Seq[Int]):Boolean=if(i.size<1)1>0 else{val(s,t)=i.tail.span(_<i(0));t.forall(_>i(0))&f(s)&f(t)}

Thanks to @Laikoni for the suggestions to make both solutions shorter.

Try it online


  1. slice (using Scala's span) the array using array's head as the slicing criteria.
  2. Confirm that the first slice of the array is less than the head and the second slice is greater than the head.
  3. recursively check that each slice also satisfies (2)
  • 1
    \$\begingroup\$ I think you don't need the space in val (s,t), true can be 1>0 and you can drop s.forall(_<i(0))& as this should already be insured by span. \$\endgroup\$ – Laikoni Oct 20 '18 at 0:46
  • 1
    \$\begingroup\$ You can call the function % and drop the space: def%(i:Seq[Int])= \$\endgroup\$ – Laikoni Oct 20 '18 at 13:07
  • \$\begingroup\$ Your solutions contain declaration of the function unlike some other. Pure expressions are quite shorter. ;) \$\endgroup\$ – Dr Y Wit Oct 23 '18 at 10:13
  • \$\begingroup\$ I was trying to port tsh's answer, but did not manage to get it short enough. Version 1. l.zipWithIndex.foldLeft(1>0){case(r,v,i)=>r&l.zip(l.tail).slice(i+1,l.length).forall(x=>l(i)>x._1|l(i)<x._2)}. Version 2. (for(i<-l.indices)yield l.zip(l.tail).slice(i+1,l.length).forall(x =>l(i)>x._1|l(i)<x._2)).forall(x=>x). Any ideas how to make these shorter? \$\endgroup\$ – Dr Y Wit Oct 23 '18 at 10:26
  • \$\begingroup\$ Algorithm in plain English: for each element do a check with all the pairs of elements going next to each other. \$\endgroup\$ – Dr Y Wit Oct 23 '18 at 10:29

05AB1E, 15 10 bytes


Port of @Lynn's Jelly answer.
-5 bytes thanks to @Emigna.

Try it online or verify all test cases.


Π            # Take all sublists of the (implicit) input-list
              #  i.e. [2,3,1] → [[2],[2,3],[2,3,1],[3],[3,1],[1]]
              #  i.e. [1,2,3,4]
              #   → [[1],[1,2],[1,2,3],[1,2,3,4],[2],[2,3],[2,3,4],[3],[3,4],[4]]
 ε      }     # Map each to:
  D           #  Duplicate the current sublist on the stack
   {          #  Sort the copy
              #   i.e. [2,3,1] → [1,2,3]
              #   i.e. [2,3,4] → [2,3,4]
    3.I       #  Get the 4th (3rd 0-indexed) permutation of this list
              #   i.e. [1,2,3] → [2,3,1]
              #   i.e. [2,3,4] → [3,4,2]
       Ê      #  Check that the lists are NOT equal
              #   i.e. [2,3,1] and [2,3,1] → 0 (falsey)
              #   i.e. [2,3,4] and [3,4,2] → 1 (truthy)
         P    # Check whether all are truthy (and output implicitly)
              #  i.e. [1,1,0,1,1,1] → 0 (falsey)
              #  i.e. [1,1,1,1,1,1,1,1,1,1] → 1 (truthy)
  • 1
    \$\begingroup\$ How about ŒεD{3.IÊ}P ? \$\endgroup\$ – Emigna Oct 19 '18 at 14:20
  • 1
    \$\begingroup\$ @Emigna Yeah, that would be indeed a lot easier... >.> Thanks! :) (And have a nice weekend.) \$\endgroup\$ – Kevin Cruijssen Oct 19 '18 at 18:39

Haskell, 41 bytes

f(h:t)=t==[]||all(>h)(snd$span(<h)t)&&f t

Try it online!

Uses Lynn's observation that it suffices to check that there's no subsequence of the for mid..high..low. This means that for each element h, the list of elements t that comes after is a block of elements <h followed by a block of elements >h (both blocks may be empty). So, the code checks that after we drop the prefix of elements <h in t, the remaining elements are all >h. Recursing checks this for each initial element h until the list is length 1.

A potential simplification is that it suffices to check for subpatterns mid..high,low where the last two are consecutive. Unfortunately, Haskell's doesn't have an short way to extract the last two elements, as can be done from the front with a pattern match a:b:c. I found a shorter solution that checks for mid,high..low, but this fails to reject inputs like [3,1,4,2].

Formatted test cases taken from Laikoni.


Japt, 14 bytes

d@sY ð_§XÃxÈ-Y

Japt Interpreter

Outputs false for BST, true for no BST.


d@                Run on each item X, return true if any aren't 0: 
  sY                  Ignore the numbers before this index
     ð_§XÃ            Get the indexes of numbers less than or equal to X
                          If it is a BST, this list will be e.g. [0,1,2...]
            -Y        Subtract the position within the index list from each index
                          eg. [0,1,2] -> [0,0,0] , [0,1,4] -> [0,0,2]
          xÈ          Sum the resulting array


All approaches are implementations of the rule shown by tsh.




(for(i<-l.indices)yield l.zip(l.tail).slice(i+1,l.size).forall(x =>l(i)>x._1|l(i)<x._2)).forall(x=>x)




(for(i<-l.indices;j<-i+1 to l.size-2)yield l(i)>l(j)|l(i)<l(j+1)).forall(x=>x)

If it must be a function and not just an expression then each line have to start with (17 bytes)


Oracle SQL, 177 bytes

with r(i,v)as(select rownum,value(t)from table(a)t)
select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r,(select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i

Since there is no boolean type in Oracle SQL, query returns either 1 or 0.

Oracle SQL 12c, 210 bytes

with function f(n ku$_objnumset,i int)return int as begin return n(i);end;
select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end)from(select value(t)c from table(powermultiset_by_cardinality(a,3))t)

It's not possible access element of the array in SQL the same way as in PL/SQL - i.e. a(i), thus function f was declared in with clause for that purpose. Otherwise solution would have been much shorter.

Other limitations

  • throws an exception for arrays shorter than 3 elements (instead of returning 1)
  • there is an assumption that powermultiset_by_cardinality preserves order even though it's not explicitly stated in the documentation

sqlplus listing

SQL> set heading off
SQL> with r(i,v)as(select rownum,value(t)from table(ku$_objnumset(6,3,2,4,5,1,8,7,9))t)
  2  select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r,
  3  (select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i
  4  /


SQL> with function f(n ku$_objnumset,i int)return int as begin return n(i);end;
  2  select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end)
  3  from(select value(t)c from table(powermultiset_by_cardinality(ku$_objnumset(6,3,2,4,5,1,8,7,9),3))t)
  4  /


SQL> with r(i,v)as(select rownum,value(t)from table(ku$_objnumset(8,3,1,6,4,7,10,14,13))t)
  2  select nvl(min(case when r.v<p.l and r.v>p.v then 0end),1)from r,
  3  (select i,lag(v)over(order by i)l,v from r)p where r.i+1<p.i
  4  /


SQL> with function f(n ku$_objnumset,i int)return int as begin return n(i);end;
  2  select min(case when f(c,1)>f(c,2)or f(c,1)<f(c,3)then 1else 0end)
  3  from(select value(t)c from table(powermultiset_by_cardinality(ku$_objnumset(8,3,1,6,4,7,10,14,13),3))t)
  4  /


Online verification apex.oracle.com


Oracle SQL, 155 bytes

with r(i,v)as(select rownum,value(t)from table(a)t)select nvl(min(case when a.v<b.v and a.v>c.v then 0end),1)r from r a,r b,r c where a.i<b.i and b.i+1=c.i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.