Skip to content

Commit c1faf71

Browse files
author
Abhijit Sarkar
committed
Minor update
1 parent e31ffe9 commit c1faf71

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

bintree/src/P68.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ object P68:
4646

4747
def preInTree(pre: List[Char], in: List[Char]): Tree[Char] =
4848
def loop(pre: List[Char], in: List[Char]): (Tree[Char], List[Char]) =
49-
pre match
50-
case head :: tail if in.size == 1 => (singleton(head), tail)
51-
case head :: tail if in.size > 1 =>
52-
val (leftIn, rightIn) = in.span(_ != head)
53-
val (left, xs) = loop(tail, leftIn)
54-
val (right, ys) = loop(xs, rightIn.tail)
49+
(pre, in) match
50+
case (head :: tail, _ :: Nil) => (singleton(head), tail)
51+
case (head :: tail, _ :: _ :: _) =>
52+
// unchecked needed since the pattern match in val is not exhaustive and may fail.
53+
// "pattern's type is more specialized than the right hand side expression's type"
54+
val (leftIn, (_ :: rightIn)) = (in.span(_ != head): @unchecked)
55+
val (left, xs) = loop(tail, leftIn)
56+
val (right, ys) = loop(xs, rightIn)
5557
(Node(head, left, right), ys)
5658
case _ => (Empty, pre)
5759

0 commit comments

Comments
 (0)