ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

二叉树的四种遍历(遍历与非遍历)

2021-10-06 22:03:19  阅读:139  来源: 互联网

标签:node 遍历 TreeNode 二叉树 与非 null root 节点


二叉树的四种遍历(遍历与非遍历)

先序遍历与后序遍历

先序遍历根节点,再遍历左子树,再遍历右子树。

后序遍历先遍历左子树,再遍历右子树,再遍历根节点。

先序遍历递归实现:

public static void preOrderByRecursion(TreeNode root) {
    // 打印节点值
    System.out.println(root.value);
    preOrder(root.left);
    preOrder(root.right);
}

先序遍历的非递归实现:

非递归实现需要借助栈这样一个数据结构,实际上递归实现也是依靠栈,只不过是隐式的。

  1. 先将根节点压入栈中。
  2. 弹出栈中的节点,将弹出节点的右子节点压入栈中,再将弹出节点的左子树压入栈中。
  3. 重复步骤2,直到栈为空。
public static void preOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> stack = new Stack<>();
    stack.push(root);
    while (!stack.empty()) {
        TreeNode node = stack.pop();
        // 打印节点值
        System.out.print(node.value + " ");
        if (node.right != null) {
            stack.push(node.right);
        }
        if (node.left != null) {
            stack.push(node.left);
        }
    }
}

后序遍历递归实现:先序遍历反过来,就不赘述了。

public static void postOrderByRecursion(TreeNode root) {
    postOrderByRecursion(root.left);
    postOrderByRecursion(root.right);
    System.out.println(root.value);
}

后序遍历非递归实现:后序遍历就是先序遍历反过来,所以需要两个栈,多出来的栈用来反向输出。

public static void postOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> s1 = new Stack<>();
    Stack<TreeNode> s2 = new Stack<>();
    s1.push(root);
    while (!s1.empty()) {
        TreeNode node = s1.pop();
        s2.push(node);
        if (node.left != null) {
            s1.push(node.left);
        }
        if (node.right != null) {
            s1.push(node.right);
        }
    }
    while (!s2.empty()) {
        System.out.println(s2.pop().value);
    }
}

中序遍历

中序遍历先遍历左子树,再遍历根节点,再遍历右子树。

递归遍历:

public static void inOrderByRecursion(TreeNode root) {
    if (root == null) {
        return;
    }
    inOrderByRecursion(root.left);
    // 打印节点值
    System.out.println(root.value);
    inOrderByRecursion(root.right);
}

非递归遍历:

  1. 将二叉树的左侧“边”从上到下依次压入栈中。
  2. 从栈中弹出节点
  3. 对以弹出节点的右子节点为根节点的子树,重复步骤1。
  4. 重复2、3步骤,直到栈为空。
public static void inOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> stack = new Stack<>();
    TreeNode cur = root;

    while (cur != null) {
        stack.push(cur);
        cur = cur.left;
    }

    while (!stack.empty()) {
        TreeNode node = stack.pop();
        System.out.println(node.value);
        cur = node.right;
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }
    }
}

层序遍历

层序遍历顾名思义就是一层一层,从左到右的遍历二叉树。需要用到队列这一数据结构。

  1. 将根节点推入队列。

  2. 从队列中取出一个节点。

  3. 先将取出节点的左子节点推入队列,再将取出节点的右子节点推入队列。

  4. 重复2、3步骤直到队列中无节点可取。

public static void floorOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    Queue<TreeNode> queue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TreeNode node = queue.poll();
        System.out.println(node.value);
        if (node.left != null) {
            queue.add(node.left);
        }
        if (node.right != null) {
            queue.add(node.right);
        }
    }
}

标签:node,遍历,TreeNode,二叉树,与非,null,root,节点
来源: https://www.cnblogs.com/funtirn/p/15374334.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有