O(N) flattening binary tree ref: http://qiita.com/Kinokkory/items/eac6a7a4789172f5cb46