?
递归版:
public class TreeNode {
public static class Tree{
private int number;
private Tree leftNode;
private Tree rightNode;
public Tree(int number, Tree leftNode, Tree rightNode) {
this.number = number;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
}
public static void creadTreeNode(){
Tree tree7 =new Tree(7,null,null);
Tree tree6 =new Tree(6,null,null);
Tree tree5 =new Tree(5,null,null);
Tree tree4 =new Tree(4,null,null);
Tree tree3 =new Tree(3,tree6,tree7);
Tree tree2 =new Tree(2,tree4,tree5);
Tree tree1 =new Tree(1,tree2,tree3);
prTreeNode(tree1);
}
public static void prTreeNode(Tree node){
if(null== node){
return;
}
System.out.println(node.number); //先序遍历
prTreeNode(node.leftNode);
//System.out.println(node.number); 中序遍历
prTreeNode(node.rightNode);
//System.out.println(node.number);后序遍历
}
public static void main(String[] args) {
creadTreeNode();
}
}
非递归:
public class TreeNode {
public static class Tree {
private int number;
private Tree leftNode;
private Tree rightNode;
public Tree(int number, Tree leftNode, Tree rightNode) {
this.number = number;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
}
public static void creadTreeNode() {
Tree tree7 = new Tree(7, null, null);
Tree tree6 = new Tree(6, null, null);
Tree tree5 = new Tree(5, null, null);
Tree tree4 = new Tree(4, null, null);
Tree tree3 = new Tree(3, tree6, tree7);
Tree tree2 = new Tree(2, tree4, tree5);
Tree tree1 = new Tree(1, tree2, tree3);
preTreeNode(tree1);
centerTreeNode(tree1);
endTreeNode(tree1);
}
/**
* 先序遍历:当前节点不为空,将当前节点压入栈。
* 1、如果栈不为空,则弹出一个节点并打印该节点信息,如果该节点右子节点不为空,将其压入栈中,如果该节点左子节点不为空,将其压入栈中。
*
* @param node
*/
public static void preTreeNode(Tree node) {
if (null != node) {
Stack<Tree> stack = new Stack<>();//创建一个栈
stack.push(node);
while (!stack.isEmpty()) {
Tree pop = stack.pop();
System.out.print(pop.number + " ");
if (null != pop.rightNode) {//栈是先进后出结构,所以先判断右节点是否为空,弹出式先弹出左节点
stack.push(pop.rightNode);
}
if (null != pop.leftNode) {
stack.push(pop.leftNode);
}
}
}
System.out.println();
}
/**
* 中序遍历:1、当前节点不为空,将当前节点放入栈中,取当前节点左子节点,再次进行第一步。
* 2、如果当前节点为空且栈不为空,则从栈中弹出一个节点并打印该节点信息,取当前节点右子节点,再次进行第一步。
*
* @param node
*/
public static void centerTreeNode(Tree node) {
if (null != node) {
Stack<Tree> stack = new Stack<>();
while (!stack.isEmpty() || null != node) {
if (null != node) {
stack.push(node);
node = node.leftNode;
} else {
Tree pop = stack.pop();
System.out.print(pop.number + " ");
node = pop.rightNode;
}
}
}
System.out.println();
}
/**
* 后序遍历:当前节点不为空,将当前节点压入栈。
* 1、如果栈不为空,则弹出一个节点并将该节点信息放入辅助栈,如果该节点左子节点不为空,将其压入栈中,如果该节点右子节点不为空,将其压入栈中。
*
* @param node
*/
public static void endTreeNode(Tree node) {
if (null != node) {
Stack<Tree> stack = new Stack<>();
stack.push(node);
Stack<Tree> fuzhu = new Stack<>();
while (!stack.isEmpty()) {
node = stack.pop();
fuzhu.push(node);
if (null != node.leftNode) {
stack.push(node.leftNode);
}
if (null != node.rightNode) {
stack.push(node.rightNode);
}
}
while (!fuzhu.isEmpty()) {
System.out.print(fuzhu.pop().number + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
creadTreeNode();
}
}
|