题目描述:请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
思路一:
s1存奇数层节点
s2存偶数层节点
用两个栈交替压入并弹出
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
int layer = 1;
Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();
s1.add(pRoot);
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
while (!s1.isEmpty() || !s2.isEmpty())
{
if (layer % 2 == 1)
{
ArrayList<Integer> tmp = new ArrayList<>();
while (!s1.isEmpty())
{
TreeNode node = s1.pop();
if (node != null)
{
tmp.add(node.val);
s2.add(node.left);
s2.add(node.right);
}
}
if (!tmp.isEmpty())
{
list.add(tmp);
layer++;
}
}
else
{
ArrayList<Integer> tmp2 = new ArrayList<>();
while (!s2.isEmpty())
{
TreeNode node2 = s2.pop();
if (node2 != null)
{
tmp2.add(node2.val);
s1.add(node2.right);
s1.add(node2.left);
}
}
if (!tmp2.isEmpty())
{
list.add(tmp2);
layer++;
}
}
}
return list;
}
}
思路二:
利用Java中的LinkedList的底层实现是双向链表的特点。
1)可用做队列,实现树的层次遍历
2)可双向遍历,奇数层时从前向后遍历,偶数层时从后向前遍历
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (pRoot == null) return res;
boolean leftToRight = true;
LinkedList<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
queue.add(null); //添加分层符
queue.add(pRoot);
while (queue.size() > 1)
{
TreeNode node = queue.poll();
if (node == null)
{
Iterator<TreeNode> iter = null;
if (leftToRight)
iter = queue.iterator();
else
iter = queue.descendingIterator();
leftToRight = !leftToRight;
while (iter.hasNext())
list.add(iter.next().val);
res.add(new ArrayList<>(list));
queue.add(null);
list.clear();
continue;
}
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
return res;
}
}
思路三:
按层序遍历分层打印的代码,添加一段判断用以倒序输出即可
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (pRoot == null) return res;
boolean leftToRight = true;
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> list = new ArrayList<>();
queue.add(pRoot);
int start = 0;
int end = 1;
while (!queue.isEmpty())
{
TreeNode node = queue.poll();
list.add(node.val);
start++;
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
if (start == end)
{
start = 0;
end = queue.size();
if (leftToRight)
res.add(list);
else
res.add(Reverse(list));
leftToRight = !leftToRight;
list = new ArrayList<>();
}
}
return res;
}
private ArrayList<Integer> Reverse(ArrayList<Integer> list)
{
ArrayList<Integer> newList = new ArrayList<>();
for (int i = list.size() - 1; i >= 0; i--)
newList.add(list.get(i));
return newList;
}
}