二叉树(先序中序和后序的实现)

import java.util.Stack;

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

        public Node(int v){
            value = v;
        }
    }

    //先序、中序、后序遍历都可以在递归序的基础加工出来
    //第一次到达一个节点就打印为先序、第二次为中序、第三次为后序
    public static void f(Node head){
        if(head ==null){
            return;
        }
        f(head.left);
        f(head.right);
    }

    //递归实现
    //先序遍历
    public static void pre1(Node head){
        if (head == null){
            return;
        }
        System.out.println(head.value);
        pre1(head.left);
        pre1(head.right);
    }

    //中序遍历
    public static void in1(Node head){
        if (head == null){
            return;
        }
        in1(head.left);
        System.out.println(head.value);
        in1(head.right);
    }

    //后序遍历
    public static void pos1(Node head){
        if (head == null){
            return;
        }
        pos1(head.left);
        pos1(head.right);
        System.out.println(head.value);
    }

    //非递归实现
    //先序
    public static void pre2(Node head){
        //弹出就打印
        //如果有右节点就压入右节点
        //如果有左节点就压入左节点
        //这样就保证了先出来左后出来右
        if(head != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.add(head);
            while (!stack.isEmpty()) {
                head = stack.pop();
                System.out.println(head.value + " ");
                if (head.right != null) {
                    stack.push(head.right);
                }
                if (head.left != null) {
                    stack.push(head.left);
                }
            }
        }
            System.out.println();
    }

    //后序,两个栈实现
    public static  void pos2(Node head){
        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.println(s2.pop().value + " ");

            }
        }

        System.out.println();
    }

    //中序
    public static void in2(Node head){
        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.println(head.value + " ");
                    head = head.right;
                }
            }
        }
        System.out.println();
    }

    //后序,一个栈实现
    public static void pos3(Node h){
        if(h != null){
            Stack<Node> stack = new Stack<Node>();
            stack.push(h);
            Node c =  null;
            while (!stack.isEmpty()){
                c = stack.peek();
                if(c.left != null && h != c.left && h != c.right){
                    stack.push(c.left);
                }else if(c.left != null && h != c.right){
                    stack.push(c.right);
                }else{
                    System.out.println(stack.pop().value + " ");
                    h = c;
                }
            }
        }
    }
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 C 语言建立二叉树实现先序中序后序遍历的代码: ```c #include <stdio.h> #include <stdlib.h> //定义二叉树节点结构体 typedef struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; } TreeNode; //创建二叉树 TreeNode* createTree() { TreeNode *root = NULL; int val; scanf("%d", &val); if (val != -1) { //输入-1表示该节点为空 root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = val; root->left = createTree(); root->right = createTree(); } return root; } //先序遍历 void preOrder(TreeNode *root) { if (root != NULL) { printf("%d ", root->val); preOrder(root->left); preOrder(root->right); } } //中序遍历 void inOrder(TreeNode *root) { if (root != NULL) { inOrder(root->left); printf("%d ", root->val); inOrder(root->right); } } //后序遍历 void postOrder(TreeNode *root) { if (root != NULL) { postOrder(root->left); postOrder(root->right); printf("%d ", root->val); } } int main() { printf("请输入二叉树先序遍历,-1表示该节点为空:\n"); TreeNode *root = createTree(); printf("二叉树先序遍历为:"); preOrder(root); printf("\n"); printf("二叉树中序遍历为:"); inOrder(root); printf("\n"); printf("二叉树后序遍历为:"); postOrder(root); printf("\n"); return 0; } ``` 输入格式: 先序遍历的每个节点值,-1表示该节点为空。 输出格式: 二叉树先序中序后序遍历结果。 示例: 输入: ``` 1 2 -1 -1 3 4 -1 -1 5 -1 -1 ``` 输出: ``` 二叉树先序遍历为:1 2 3 4 5 二叉树中序遍历为:2 1 4 3 5 二叉树后序遍历为:2 4 5 3 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值