Implement layered binary tree algorithm