转载自:https://www.cnblogs.com/yaobolove/p/6213936.html
二叉树的遍历一直都是面试的考点,今天来写一下递归的遍历实现,这里主要是想写点东西记录下来,所以很多很多地方都是雷同的。
这篇文章就是摘抄:
以下是我要解析的一个二叉树的模型形状

Java二叉树的前序、中序、后序遍历--递归

接下来废话不多直接上代码

一种是用递归的方法,另一种是用堆栈的方法:

首先创建一棵树:

public class Node {  
    private int data;  
    private Node leftNode;  
    private Node rightNode;  
    public Node(int data, Node leftNode, Node rightNode){  
        this.data = data;  
        this.leftNode = leftNode;  
        this.rightNode = rightNode;  
    }  
  
    public int getData() {  
        return data;  
    }  
    public void setData(int data) {  
        this.data = data;  
    }  
    public Node getLeftNode() {  
        return leftNode;  
    }  
    public void setLeftNode(Node leftNode) {  
        this.leftNode = leftNode;  
    }  
    public Node getRightNode() {  
        return rightNode;  
    }  
    public void setRightNode(Node rightNode) {  
        this.rightNode = rightNode;  
    }  
}
public class BinaryTree {  
    /** 
     * @author yaobo
     * 二叉树的先序中序后序排序 
     */  
    public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错  
        Node J = new Node(8, null, null);  
        Node H = new Node(4, null, null);  
        Node G = new Node(2, null, null);  
        Node F = new Node(7, null, J);  
        Node E = new Node(5, H, null);  
        Node D = new Node(1, null, G);  
        Node C = new Node(9, F, null);  
        Node B = new Node(3, D, E);  
        Node A = new Node(6, B, C);  
        return A;   //返回根节点  
    }
    
    public void printNode(Node node){  
        System.out.print(node.getData());  
    }  
    public void theFirstTraversal(Node root) {  //先序遍历  
        printNode(root);  
        if (root.getLeftNode() != null) {  //使用递归进行遍历左孩子  
            theFirstTraversal(root.getLeftNode());  
        }  
        if (root.getRightNode() != null) {  //递归遍历右孩子  
            theFirstTraversal(root.getRightNode());  
        }  
    }  
    public void theInOrderTraversal(Node root) {  //中序遍历  
        if (root.getLeftNode() != null) {  
            theInOrderTraversal(root.getLeftNode());  
        }  
        printNode(root);  
        if (root.getRightNode() != null) {  
            theInOrderTraversal(root.getRightNode());  
        }  
    }
    
    
    public void thePostOrderTraversal(Node root) {  //后序遍历  
        if (root.getLeftNode() != null) {  
            thePostOrderTraversal(root.getLeftNode());  
        }  
        if(root.getRightNode() != null) {  
            thePostOrderTraversal(root.getRightNode());  
        }  
        printNode(root);  
    }  
      
    public static void main(String[] args) {  
        BinaryTree tree = new BinaryTree();  
        Node root = tree.init();  
        System.out.println("先序遍历");  
        tree.theFirstTraversal(root);  
        System.out.println("");  
        System.out.println("中序遍历");  
        tree.theInOrderTraversal(root);  
        System.out.println("");  
        System.out.println("后序遍历");  
        tree.thePostOrderTraversal(root);  
        System.out.println("");  
    }  
}

相关文章:

  • 2021-06-18
  • 2021-12-29
  • 2021-08-08
  • 2021-04-10
  • 2022-12-23
  • 2021-12-23
  • 2022-01-22
  • 2021-12-03
猜你喜欢
  • 2021-07-30
  • 2021-04-24
  • 2021-12-12
  • 2022-02-04
  • 2022-12-23
  • 2021-09-26
  • 2021-12-03
相关资源
相似解决方案