左神视频day05——题目一:先中后序遍历,递归和非递归方式

实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式

import java.util.Stack;

public class Code_01_PreInPosTraversal {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    //递归版先中后序遍历
    public static void preOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        System.out.print(head.value + " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }

    public static void inOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        inOrderRecur(head.left);
        System.out.print(head.value + " ");
        inOrderRecur(head.right);
    }

    public static void posOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        posOrderRecur(head.left);
        posOrderRecur(head.right);
        System.out.print(head.value + " ");
    }

    //非递归版先中后序遍历

    /**
     * 先序遍历
     * 往栈中压入根结点
     * 弹出栈中一个结点并打印
     * 压入刚弹出结点的右结点和左结点
     * 弹出栈中一个结点并打印
     */
    public static void preOrderUnRecur(Node head) {
        System.out.print("pre-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.add(head);
            while (!stack.isEmpty()) {
                head = stack.pop();
                System.out.print(head.value + " ");
                if (head.right != null) {
                    stack.push(head.right);
                }
                if (head.left != null) {
                    stack.push(head.left);
                }
            }
        }
        System.out.println();
    }

    /**
     * 中序遍历
     * 当前结点不为空时,压入当前结点,当前结点指针向它左结点移动
     * 当前结点为空、栈不为空时,弹出栈结点并打印,当前结点指针向栈结点的右结点移动
     */
    public static void inOrderUnRecur(Node head) {
        System.out.print("in-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            while (!stack.isEmpty() || head != null) {
                if (head != null) {
                    stack.push(head);
                    head = head.left;
                } else {
                    head = stack.pop();
                    System.out.print(head.value + " ");
                    head = head.right;
                }
            }
        }
        System.out.println();
    }

    /**
     * 后序遍历
     * 由前面的先序遍历,中左右,改为中右左,然后放入栈中逆序,得到左右中,即后序遍历
     */
    public static void posOrderUnRecur(Node head) {
        System.out.print("pos-order: ");
        if (head != null) {
            Stack<Node> s1 = new Stack<Node>();
            Stack<Node> s2 = new Stack<Node>();
            s1.push(head);
            while (!s1.isEmpty()) {
                head = s1.pop();
                s2.push(head);
                if (head.left != null) {
                    s1.push(head.left);
                }
                if (head.right != null) {
                    s1.push(head.right);
                }
            }
            while (!s2.isEmpty()) {
                System.out.print(s2.pop().value + " ");
            }
        }
        System.out.println();
    }


    public static void main(String[] args) {
        Node head = new Node(5);
        head.left = new Node(3);
        head.right = new Node(8);
        head.left.left = new Node(2);
        head.left.right = new Node(4);
        head.left.left.left = new Node(1);
        head.right.left = new Node(7);
        head.right.left.left = new Node(6);
        head.right.right = new Node(10);
        head.right.right.left = new Node(9);
        head.right.right.right = new Node(11);

        // recursive
        System.out.println("==============recursive==============");
        System.out.print("pre-order: ");
        preOrderRecur(head);
        System.out.println();
        System.out.print("in-order: ");
        inOrderRecur(head);
        System.out.println();
        System.out.print("pos-order: ");
        posOrderRecur(head);
        System.out.println();

        // unrecursive
        System.out.println("============unrecursive=============");
        preOrderUnRecur(head);
        inOrderUnRecur(head);
        posOrderUnRecur(head);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为您解答这个问题。 首需要了解二叉树的和后算法的概念,访问二叉树的左子树,再访问根结点,最后访问右子树;后则是访问左子树,再访问右子树,最后访问根结点。归算法就是利用函数自己调用自己的方式实现,归算法就是利用栈的方式模拟实现。 以下是这个问题的具体实现方法: 首,需要定义二叉树的结构体和节点: ``` typedef struct TreeNode { int val; //节点的值 struct TreeNode *left; //指向左子树的指针 struct TreeNode *right; //指向右子树的指针 }TreeNode, *pTreeNode; ``` 然后是归方法: ``` void inorderTraversalRecursion(pTreeNode root) { if (root) { inorderTraversalRecursion(root->left); //访问左子树 printf("%d ", root->val); //再访问当前节点 inorderTraversalRecursion(root->right); //最后访问右子树 } } ``` 归方法: ``` void inorderTraversalNonRecursion(pTreeNode root) { pTreeNode stack[100]; //定义栈 int top = -1; pTreeNode p = root; while (top != -1 || p) { //当栈不为空或者p不为空时循环 if (p) { //如果p不为空,则一直向左走并入栈 stack[++top] = p; p = p->left; } else { //否则出栈一个元素,访问该节点,然后向右遍 p = stack[top--]; printf("%d ", p->val); p = p->right; } } } ``` 接下来是后归方法: ``` void postorderTraversalRecursion(pTreeNode root) { if (root) { postorderTraversalRecursion(root->left); //访问左子树 postorderTraversalRecursion(root->right); //再访问右子树 printf("%d ", root->val); //最后访问当前节点 } } ``` 后归方法: ``` void postorderTraversalNonRecursion(pTreeNode root) { pTreeNode stack[100]; //定义栈 int top = -1; pTreeNode p = root, q = NULL; while (top != -1 || p) { //当栈不为空或者p不为空时循环 if (p) { //如果p不为空,则一直向左走并入栈 stack[++top] = p; p = p->left; } else { p = stack[top]; if (p->right && p->right != q) { //如果右子树存在且未被访问,则向右遍 p = p->right; stack[++top] = p; p = p->left; } else { //否则出栈一个元素,访问该节点 printf("%d ", p->val); q = p; top--; p = NULL; } } } } ``` 以上就是实现二叉树的和后归和归算法的详细步骤,希望对您有所帮助。如有不懂之处可以随时跟我交流。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值