二叉树的各种遍历
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;
/**
* 二叉树的遍历:
* 递归和非递归先序、中序、后序遍历
* 层次遍历:不分层层次遍历、分层层次遍历、之字形遍历
*/
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode( int val){
this.val = val;
}
}
public class TreeNodeTraversal {
//先序遍历的递归实现
public static void preOrderRecur(TreeNode root){
if (root == null){
return;
}
System.out.print(root.val + " ");
preOrderRecur(root.left);
preOrderRecur(root.right);
}
//先序遍历的非递归实现
public static void preOrderUnRecur(TreeNode root){
if (root != null){
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()){
root = stack.pop();
System.out.print(root.val + " ");
if (root.right != null){
stack.push(root.right);
}
if (root.left != null){
stack.push(root.left);
}
}
}
}
//中序遍历的递归实现
public static void inOrderRecur(TreeNode root){
if (root == null){
return;
}
inOrderRecur(root.left);
System.out.print(root.val + " ");
inOrderRecur(root.right);
}
//中序遍历的非递归实现
public static void inOrderUnRecur(TreeNode root){
if(root != null){
Stack<TreeNode> stack = new Stack<>();
while (!stack.empty() || root != null){
if (root != null){
stack.push(root);
root = root.left;
}else {
root = stack.pop();
System.out.print(root.val + " ");
root = root.right;
}
}
}
}
//后序遍历的递归实现
public static void postOrderRecur(TreeNode root){
if (root == null){
return;
}
inOrderRecur(root.left);
inOrderRecur(root.right);
System.out.print(root.val + " ");
}
//后序遍历的非递归实现
public static void postOrderUnRecur(TreeNode root){
if (root != null){
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(root);
while (!stack1.empty()){
root = stack1.pop();
stack2.push(root);
if (root.left != null){
stack1.push(root.left);
}
if (root.right != null){
stack1.push(root.right);
}
}
while (!stack2.empty()){
System.out.print(stack2.pop().val + " ");
}
}
}
//不分层的层次遍历
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();//保存结果
if (root == null){
return result;
}
//用ArrayList来模拟队列
ArrayList<TreeNode> queue = new ArrayList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode temp = queue.remove(0);
result.add(temp.val);
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
return result;
}
//分层层次遍历
public ArrayList<ArrayList<Integer> > PrintFC(TreeNode root) {
ArrayList<ArrayList<Integer> > result = new ArrayList<>();//保存结果
if (root == null){
return result;
}
int count = 0;//保存当前行的节点个数
ArrayList<Integer> list = null;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()){
list = new ArrayList<>();
count = queue.size();//当前行的节点数
while (count-- > 0){
TreeNode temp = queue.poll();
list.add(temp.val);
if (temp.left != null){
queue.offer(temp.left);
}
if (temp.right != null){
queue.offer(temp.right);
}
}
result.add(list);
}
return result;
}
//分层之字形遍历
public static ArrayList<ArrayList<Integer> > PrintZ(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> result = new ArrayList<>();//保存最终结果
if(pRoot==null){
return result;
}
Stack<TreeNode>[] stacks = new Stack[2];//保存中间节点
stacks[0] = new Stack<>();
stacks[1] = new Stack<>();
int flag = 0;
stacks[0].push(pRoot);
ArrayList<Integer> list = new ArrayList<>();
while(!stacks[flag].empty()){//待输出元素不为空
TreeNode temp = stacks[flag].pop();
list.add(temp.val);
//根据当前的打印方向,判断下一行的入栈顺序
if(flag==0){
if(temp.left!=null){
stacks[1-flag].push(temp.left);
}
if(temp.right!=null){
stacks[1-flag].push(temp.right);
}
}else{
if(temp.right!=null){
stacks[1-flag].push(temp.right);
}
if(temp.left!=null){
stacks[1-flag].push(temp.left);
}
}
//当前行的元素打印完毕
if(stacks[flag].empty()){
result.add(list);
list = new ArrayList<>();
flag = 1-flag;
}
}
return result;
}
public static void main(String[] args) {
TreeNode node1 = new TreeNode(8);
TreeNode node2 = new TreeNode(10);
TreeNode node3 = new TreeNode(6);
TreeNode node4 = new TreeNode(5);
TreeNode node5 = new TreeNode(7);
TreeNode node6 = new TreeNode(9);
TreeNode node7 = new TreeNode(11);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node2.right = node5;
node3.left = node6;
node3.right = node7;
System.out.println(PrintZ(node1));
}
}