Kara walks from left to right and from right to left, alternatively, and "inverts" the current row of the image. The main program is the following:

walkingLeft2Right = true

while not (kara.treeFront and (kara.treeLeft or kara.treeRight))
  if walkingLeft2Right
    left2right(kara)
  else
    right2left(kara)
  end
  walkingLeft2Right = !walkingLeft2Right
end