We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e31ffe9 commit c1faf71Copy full SHA for c1faf71
bintree/src/P68.scala
@@ -46,12 +46,14 @@ object P68:
46
47
def preInTree(pre: List[Char], in: List[Char]): Tree[Char] =
48
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)
+ (pre, in) match
+ case (head :: tail, _ :: Nil) => (singleton(head), tail)
+ case (head :: tail, _ :: _ :: _) =>
+ // unchecked needed since the pattern match in val is not exhaustive and may fail.
+ // "pattern's type is more specialized than the right hand side expression's type"
+ val (leftIn, (_ :: rightIn)) = (in.span(_ != head): @unchecked)
55
+ val (left, xs) = loop(tail, leftIn)
56
+ val (right, ys) = loop(xs, rightIn)
57
(Node(head, left, right), ys)
58
case _ => (Empty, pre)
59
0 commit comments