algorithm to traverse a tree diagonally