Paste_Image.png
My code:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
if (root == null)
return result;
Stack<TreeNode> s = new Stack<TreeNode>();
Stack<TreeNode> sSon = new Stack<TreeNode>();
s.push(root);
ArrayList<Integer> sample = new ArrayList<Integer>();
int count = 2;
while(!s.isEmpty()) {
TreeNode temp = s.pop();
sample.add(temp.val);
if (s.isEmpty()) {
if (count % 2 == 0) {
if (temp.left != null)
sSon.push(temp.left);
if (temp.right != null)
sSon.push(temp.right);
}
else {
if (temp.right != null)
sSon.push(temp.right);
if (temp.left != null)
sSon.push(temp.left);
}
result.add(sample);
sample = new ArrayList<Integer>();
s = sSon;
sSon = new Stack<TreeNode>();
count++;
}
else {
if (count % 2 == 0) {
if (temp.left != null)
sSon.push(temp.left);
if (temp.right != null)
sSon.push(temp.right);
}
else {
if (temp.right != null)
sSon.push(temp.right);
if (temp.left != null)
sSon.push(temp.left);
}
}
}
return result;
}
}
My test result:
Paste_Image.png
這道題目也需要動點腦子,總算還是做出來了。
其實和上面那道題目, Binary Tree Level Order Traversal 很類似,只不過改變了遍歷的順序。于是我決定改用Stack。但是也有些細節(jié)需要注意。
比如, 1,2,3,4,#,#,5
如果簡單的把queue換成stack,會這樣
第一次壓入 1, 彈出。
第二次壓入 3, 2, 彈出,所以訪問順序會是 2, 3. 就錯了。
所以插入順序應該反一反,壓入 2, 3. 然后彈出3, 2.
第三次會壓入5, 4. 彈出 4, 5.
這樣的順序就對了。
**
總結: stack, zigzag level traversal
**
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
ArrayList<List<Integer>> ret = new ArrayList<List<Integer>>();
if (root == null)
return ret;
int counter = 0;
Stack<TreeNode> s = new Stack<TreeNode>();
s.push(root);
while(!s.isEmpty()) {
ArrayList<Integer> group = new ArrayList<Integer>();
int size = s.size();
Stack<TreeNode> help = new Stack<TreeNode>();
if (counter % 2 == 0) {
for (int i = 0; i < size; i++) {
TreeNode temp = s.pop();
group.add(temp.val);
if (temp.left != null)
help.push(temp.left);
if (temp.right != null)
help.push(temp.right);
}
}
else {
for (int i = 0; i < size; i++) {
TreeNode temp = s.pop();
group.add(temp.val);
if (temp.right != null)
help.push(temp.right);
if (temp.left != null)
help.push(temp.left);
}
}
s = help;
counter++;
ret.add(group);
}
return ret;
}
}
想簡單了一點,不能只用一個stack實現(xiàn),得用兩個。
不難。
有時候做題不能想當然。像這道題目,s彈出元素對其操作時,再次把子結點插入該棧中,造成了問題。一開始盲目想的時候都沒有想到。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if (root == null) {
return ret;
}
Stack<TreeNode> curr = new Stack<TreeNode>();
Stack<TreeNode> next = new Stack<TreeNode>();
curr.push(root);
int counter = 0;
while (!curr.isEmpty()) {
int size = curr.size();
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
TreeNode node = curr.pop();
list.add(node.val);
if (counter % 2 == 0) {
if (node.left != null) {
next.push(node.left);
}
if (node.right != null) {
next.push(node.right);
}
}
else {
if (node.right != null) {
next.push(node.right);
}
if (node.left != null) {
next.push(node.left);
}
}
}
ret.add(list);
counter++;
curr = next;
next = new Stack<TreeNode>();
}
return ret;
}
}
沒什么好說的,感覺就是體力活,沒難度。
Anyway, Good luck, Richardo! -- 09/06/2016