二叉树遍历详解

二叉树的遍历

二叉树有四种遍历,前中后序和层序遍历
下面的代码是b站学习时做的笔记
这里给出前中后序遍历的方法

递归方法:


// 内部节点类
public static class TreeNode {
   TreeNode left;
   int val;
   TreeNode right;

   public TreeNode(int val) {
       this.val = val;
   }

   public TreeNode(TreeNode left, int val, TreeNode right) {
       this.left = left;
       this.val = val;
       this.right = right;
   }
}
//    TreeNode root;

/**
* <h3>前序遍历</h3>
*
* @param node 节点
*/
public static void preOrder(TreeNode node) {
   if (node == null) {
       return;
   }
   System.out.print(node.val + "\t"); // 值
   preOrder(node.left); // 左
   preOrder(node.right); // 右
}

/**
* <h3>中序遍历</h3>
*
* @param node 节点
*/
public static void inOrder(TreeNode node) {
   if (node == null) {
       return;
   }
   inOrder(node.left); // 左
   System.out.print(node.val + "\t"); // 值
   inOrder(node.right); // 右
}

/**
* <h3>后序遍历</h3>
*
* @param node 节点
*/
public static void postOrder(TreeNode node) {
   if (node == null) {
       return;
   }
   postOrder(node.left); // 左
   postOrder(node.right); // 右
   System.out.print(node.val + "\t"); // 值
}

非递归方法: 用栈来模拟

/**
 * 非递归实现前序遍历
 *
 * @param root
 */
public static void preOrderByStack(TreeNode root) {
    LinkedList<TreeNode> list = new LinkedList<>();
    System.out.println();
    TreeNode curr = root;
    while (curr != null || !list.isEmpty()) {
        if (curr != null) {
            System.out.print(curr.val + "\t");
            list.push(curr);
            curr = curr.left;
        } else {
            TreeNode pop = list.pop();
            curr = pop.right;
        }
    }
}

/**
 * 非递归实现中序遍历
 *
 * @param root
 */
public static void inOrderByStack(TreeNode root) {
    LinkedList<TreeNode> list = new LinkedList<>();
    System.out.println();
    TreeNode curr = root;
    while (curr != null || !list.isEmpty()) {
        if (curr != null) {
            list.push(curr);
            curr = curr.left;
        } else {
            TreeNode pop = list.pop();
            System.out.print(pop.val + "\t");
            curr = pop.right;
        }
    }
}

/**
 * 非递归实现后序遍历
 * 难点
 * @param root
 */
public static void postOrderByStack(TreeNode root) {
    LinkedList<TreeNode> list = new LinkedList<>();

    System.out.println();

    TreeNode curr = root;
    TreeNode pop = null;
    while (curr != null || !list.isEmpty()) {
        if (curr != null) {
            list.push(curr);
            curr = curr.left;
        } else {
            TreeNode peek = list.peek();
            if(peek.right == null || peek.right == pop){
                pop = list.pop();
                System.out.print(pop.val + "\t");
            }else{
                curr = peek.right;
            }
        }
    }
}

/**
 * 三种遍历方式都能用一个模板套
 * @param root
 */
public static void allOrderByStack(TreeNode root) {
    LinkedList<TreeNode> list = new LinkedList<>();

    System.out.println();

    TreeNode curr = root;
    TreeNode pop = null;
    while (curr != null || !list.isEmpty()) {
        if (curr != null) {
            list.push(curr);
            // 待处理左子树
            System.out.println("前序: "+curr.val);
            curr = curr.left;
        } else {
            TreeNode peek = list.peek();

            if(peek.right == null){
                pop = list.pop();
                System.out.println("后序: "+pop.val);
                System.out.println("中序: "+pop.val);
            }else if(peek.right == pop){
                pop = list.pop();
                System.out.println("后序: "+pop.val);
            }else{
                // 待处理右子树
                System.out.println("中序: "+peek.val);
                curr = peek.right;
            }
        }
    }
}

主函数,可供自己调试

public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(new TreeNode(new TreeNode(1), 2, new TreeNode(3))
                , 4
                , new TreeNode(new TreeNode(5), 6, new TreeNode(7)));

        preOrder(treeNode);
        preOrderByStack(treeNode);
        System.out.println();
        inOrder(treeNode);
        inOrderByStack(treeNode);
        System.out.println();
        postOrder(treeNode);
        postOrderByStack(treeNode);
        System.out.println();
        allOrderByStack(treeNode);
    }

这里用后序遍历的代码作为模板, 来进行三种方式的遍历, 非递归实现二叉树的遍历中后序遍历较难, 需要自己动手模拟

如有错误,欢迎评论区指正

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值