代码中的图长这个样子↓↓↓
//非递归里的两部分代码思路过程完全一样,只不过是后者要求返回值为list
import java.util.*;
import java.util.Queue;
class Node{
public char val;
public Node left;//左孩子
public Node right;//右孩子
//为什么把val的类型定义为char类型,输出的就是字母呢???定义为int类型输出的就是ASCII码对应的值???
//首先因为你蠢,其次,一棵二叉树结点的值本来是字母,你把val定义为整型的,它不输出数字,难道输出字母吗?
//要输出字母肯定要定义为字符类型啊!!!<=我犯的错误=>特别容易忽视的问题!
public Node(char val){
this.val=val;
}
}
public class BinaryTree {
/*private static class Node { //这些参数定义在在类方法外或者内都可以,只是有些需稍加修改
char val;
Node left;
Node right;
public Node(char val) {
this.val = val;
}
}*/
// 1、构造二叉树
public Node buildTree(){
Node A=new Node('A');
Node B=new Node('B');
Node C=new Node('C');
Node D=new Node('D');
Node E=new Node('E');
Node F=new Node('F');
Node G=new Node('G');
Node H=new Node('H');
A.left=B;A.right=C;B.left=D;B.right=E;//这里是构造二叉树的关键
C.left=F;C.right=G;E.right=H;
return A;
}
// 2、(1)递归的前序遍历(根 左 右)
public void preOrderTraversal(Node root){
if(root==null){ //遍历之前肯定要先判断二叉树是否为空
return;
}
System.out.printf(root.val+" ");//先打印根节点,然后递归分别打印左右结点(中序遍历,后序遍历同理)
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
//对于二叉树的非递归遍历,操作都差不多,会重复,所以得用循环,和栈
// (2)非递归的前序遍历(根 左 右)
//根据前序遍历的思想,首先将结点压入栈,到它该出来的时候再将它出栈
public void preOrderTraversalNor(Node root) {
if (root == null) {
return;
}
Stack<Node> stack = new Stack<>();
Node cur = root;//将根结点赋给cur
while (cur != null||!stack.empty()) {//注意循环的条件
while (cur != null) {
stack.push(cur);//将cur入栈
System.out.print(cur.val + " ");//然后打印cur
cur = cur.left;//再让cur=cur.lrft
}
Node top = stack.pop();
cur = top.right;
}
}
//非递归前序遍历(返回值为list)
public List<Character> preOrderTraversalNor1(Node root) {
List<Character> list=new ArrayList<>();
if (root == null) {
return list;
}
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null||!stack.empty()) {
while (cur != null) {
stack.push(cur);//将cur入栈
//System.out.print(cur.val + " ");
list.add(cur.val);//将cur.val放进队列
cur = cur.left;
}
Node top = stack.pop();//将出栈的元素赋给top,然后继续判断top的右子树
cur = top.right;
}
return list;
}
// 3、(1)递归的中序遍历(左 根 右)
public void inOrderTraversal(Node root){
if(root==null){
return;
}
inOrderTraversal(root.left);
System.out.printf(root.val+" ");
inOrderTraversal(root.right);
}
//思路想不起来,代码又看不懂的时候,自个画个二叉树,画个栈,将代码在纸上跑一遍,应该能看懂
// (2)非递归的中序遍历<思想与前序遍历类似,只是值放进队列的时间不同>
//根据中序遍历的思想,首先将结点压入栈,到它该出来的时候再将它出栈
public void inOrderTraversalNor(Node root) {
if (root == null) {
return;
}
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null||!stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
Node top = stack.pop();
System.out.print(top.val + " ");
cur = top.right;
}
}
//非递归中序遍历(返回值为list)
public List<Character> inOrderTraversalNor1(Node root) {
List<Character> list=new ArrayList<>();
if (root == null) {
return list;
}
Stack<Node> stack = new Stack<>();
Node cur = root;
while (cur != null||!stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
Node top = stack.pop();
//System.out.print(top.val + " ");
list.add(top.val);
cur = top.right;
}
return list;
}
// 4、(1)递归的后序遍历(左 右 根)
public void postOrderTraversal(Node root){
if(root==null){
return;
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.printf(root.val+" ");
}
// (2)非递归后序遍历<后序遍历比较复杂>且耐心!
public void postOrderTraversalNor(Node root){
if (root == null) {
return;
}
Stack<Node> stack = new Stack<>();
Node cur = root;
Node prev=null; //定义了一个前驱结点是为了防止重复打印
while (cur != null||!stack.empty()) {
while(cur!=null){
stack.push(cur);//当cur不为空时,就将该结点及该结点的左边入栈,如此循环,直到把最左边的全部入栈
cur=cur.left;
}
cur=stack.peek();//取当前栈顶元素
if(cur.right==null||cur.right==prev){ //判断当前栈顶元素右孩子为空的情况,这样可直接打印当前栈顶元素
System.out.printf(cur.val+" ");
stack.pop();
prev=cur;
cur=null;
}else{//若当前栈顶元素的右孩子不为空,
cur=cur.right;
}
}
}
//非递归后序遍历(返回值为list)
public List<Character> postOrderTraversalNor1(Node root){
List<Character> list=new ArrayList<>();
if (root == null) {
return list;
}
Stack<Node> stack = new Stack<>();
Node cur = root;
Node prev=null;
while (cur != null||!stack.empty()) {
while(cur!=null){
stack.push(cur);
cur=cur.left;
}
cur=stack.peek();//取当前栈顶元素
if(cur.right==null||cur.right==prev){ //判断当前栈顶元素右孩子为空的情况,这样可直接打印当前栈顶元素
//System.out.printf(cur.val+" ");
list.add(cur.val);
stack.pop();//出栈
prev=cur;
cur=null;
}else{//若当前栈顶元素的右孩子不为空,
cur=cur.right;
}
}
return list;
}
public static void main(String[] args) {
BinaryTree binaryTree=new BinaryTree();
Node root=binaryTree.buildTree();
System.out.printf("前序遍历:");
binaryTree.preOrderTraversal(root);
System.out.println();
System.out.printf("非递归前序遍历:");
binaryTree.preOrderTraversalNor(root);
System.out.println();
System.out.println("非递归前序遍历1:"+binaryTree.preOrderTraversalNor1(root));
System.out.println();//区分前序遍历和中序遍历
System.out.printf("中序遍历:");
binaryTree.inOrderTraversal(root);
System.out.println();
System.out.printf("非递归中序遍历:");
binaryTree.inOrderTraversalNor(root);
System.out.println();
System.out.println("非递归中序遍历1:"+binaryTree.inOrderTraversalNor1(root));
System.out.println();//区分中序遍历和后序遍历
System.out.printf("后序遍历:");
binaryTree.postOrderTraversal(root);
System.out.println();
System.out.printf("非递归后序遍历:");
binaryTree.postOrderTraversalNor(root);
System.out.println();
System.out.println("非递归后序遍历1:"+binaryTree.postOrderTraversalNor1(root));
}
}
//问题问着问着,自己就解决了:为什么没有返回值的方法,在最后输出的时候不能将函数写在System.out.println();中?
//是因为,没有返回值的方法体中就有System.out.println();,难道还需要再用一次System.out.println();吗,显然不用!
执行结果: