[Java][LeetCode][BFS] Binary Tree Zigzag Level Order Traversa #103
Given the root
of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]
輸出是依層輸出,所以BFS起手式先來,題目要求要之字形,也就是說順排->逆排->順排->逆排...第一次我誤會成是要InOrder->PostOrder->InOrder->PostOrder…於是我就放了flag,true與false輪流來改變進Queue的順序,後來發現完全會錯意了...就改了順排逆排的方式,逆排的時候把list做反轉。最後再試試在LeetCode上面做test suit,剛好是個TreeNode跟雙層List都要自己做出來的例子...
output is dependent on Layer, so this problem is BFS. I need to output order and reverse order alternate. When this term is reverse order, I need to reverser list. The first time, I had wrong direction. I think is InOrder and PostOrder alternate.
This code include Test Suit at LeetCode.