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

目录

前序遍历

中序遍历

后序遍历

测试部分代码:

打印结果:

代码整合:

Test部分

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+ " ");

测试部分代码:

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();
    }
}

打印结果:

 

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

代码整合:

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+ " ");
    }

}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西西¥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值