递归实现二叉树的三种遍历(Java版)

目录

前序遍历

中序遍历

后序遍历

测试部分代码:

打印结果:

代码整合:

Test部分

TestBinaryTree部分


*这里的前、中、后序遍历是针对于根节点来说的。

首先手动构建一个二叉树

递归实现二叉树的三种遍历(Java版)_第1张图片

 

public class TestBinaryTree {

    static class TreeNode{
        public char val;
        public  TreeNode left;//存储左孩子的引用
        public  TreeNode right;//存储右孩子的引用
        public  TreeNode(char val){
            this.val = val;
        }
    }


   //手动建立结点
    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

//记录各个节点的位置
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;

    }


}

前序遍历

根结点-左子树-右子树

   public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
       System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
   }

中序遍历

左子树-根结点-右节点

    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
       inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

后序遍历

左子树-右子树-根结点

 public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+ " ");

测试部分代码:

public class Test {
    public static void main(String[] args) {
        TestBinaryTree testBinaryTree = new TestBinaryTree();
        TestBinaryTree.TreeNode root  = testBinaryTree.createTree();
        
       //前序
        testBinaryTree.preOrder(root);
        System.out.println();
        //中序
        testBinaryTree.inOrder(root);
        System.out.println();
        //后序
        testBinaryTree.postOrder(root);
        System.out.println();
    }
}

打印结果:

递归实现二叉树的三种遍历(Java版)_第2张图片

 递归实现二叉树的三种遍历(Java版)_第3张图片

 和自己推到的前、中、后序遍历的结果是相同的。

代码整合:

Test部分

public class Test {
    public static void main(String[] args) {
        TestBinaryTree testBinaryTree = new TestBinaryTree();
        TestBinaryTree.TreeNode root  = testBinaryTree.createTree();
        testBinaryTree.preOrder(root);
        System.out.println();
        testBinaryTree.inOrder(root);
        System.out.println();
        testBinaryTree.postOrder(root);
        System.out.println();
    }
}

TestBinaryTree部分

public class TestBinaryTree {

    static class TreeNode{
        public char val;
        public  TreeNode left;//存储左孩子的引用
        public  TreeNode right;//存储右孩子的引用
        public  TreeNode(char val){
            this.val = val;
        }
    }

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;

    }
    //前序遍历
   public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
       System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
   }

   //中序遍历
    public void inOrder(TreeNode root){
        if(root == null){
            return;
        }
       inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+ " ");
    }

}

你可能感兴趣的:(数据结构,数据结构,算法,java,idea)