Traversing a binary tree in JavaScript