树的遍历-先中后层次以及重构

package tree;

import java.util.*;

public class 树的遍历 {
    /**
     * 基础的先序,中序,后序
     * 从图中引进的层次遍历,蛇形遍历
     * 基于先序中序重构造二叉树
     */
    //定义树结点
    public static class Node {
        public int value;
        public Node left;
        public Node right;

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

    //preOrder
    public static void preOrderrecur(Node node) {
        if (node == null) return;
        System.out.println(node.value);
        preOrderrecur(node.left);
        preOrderrecur(node.right);
    }

    public static void preOrderStack(Node node) {
        if (node != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.push(node);
            while (!stack.isEmpty()) {
                Node cur = stack.pop();
                System.out.println(cur.value + " ");
                if (cur.right != null) {
                    stack.push(cur.right);
                }
                if (cur.left != null) {
                    stack.push(cur.left);
                }
            }
        }
    }

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

    public static void inOrderStack(Node node) {
        if (node != null) {
            Stack<Node> stack = new Stack<>();
            stack.push(node);
            while (!stack.isEmpty() || node != null) {
                if (node != null) {
                    stack.push(node);
                    node = node.left;
                } else {
                    node = stack.pop();
                    System.out.println(node.value + " ");
                    node = node.right;
                }
            }
        }
        System.out.println();
    }

    //postOrder
    public static void postOrderrecur(Node node) {
        if (node == null) return;
        postOrderrecur(node.left);
        postOrderrecur(node.right);
        System.out.print(node.value + " ");
    }

    public static void postOrderStack(Node node) {
        if (node != null) {
            Stack<Node> stack1 = new Stack<>();
            Stack<Node> stack2 = new Stack<>();
            stack1.push(node);
            while (!stack1.isEmpty()) {
                Node cur = stack1.pop();
                stack2.push(cur);
                if (cur.left != null) {
                    stack1.push(cur.left);
                }
                if (cur.right != null) {
                    stack1.push(cur.right);
                }
            }
            while (!stack2.isEmpty()) {
                Node cur = stack2.pop();
                System.out.print(cur.value + " ");
            }
        }
    }

    //层次遍历 就是bfs 用queue
    public static void bfs(Node node){
        if(node == null) return;
        Queue<Node> q = new LinkedList<>();
        q.add(node);
        while(!q.isEmpty()){
            Node cur = q.poll();
            System.out.println(cur.value+ " ");
            if(cur.left != null){
                q.add(cur.left);
            }
            if(cur.right != null){
                q.add(cur.right);
            }
        }
        System.out.println();
    }

    //蛇形层次遍历 就是层次遍历加一个flag
    public static void snakebfs(Node node){
        if(node == null) return;
        Queue<Node> q = new LinkedList<>();
        q.add(node);
        boolean flag = true;
        while(!q.isEmpty()){
            int len = q.size();
            List<Node> temp = new LinkedList<>();
            for (int i = 0; i < len; i++) {
                Node cur = q.poll();
                temp.add(cur);
                if(cur.left != null){
                    q.add(cur.left);
                }
                if(cur.right != null){
                    q.add(cur.right);
                }
            }
            if(flag){
                for(Node n : temp){
                    System.out.print(n.value);
                }
                flag = !flag;
            }else {
                Collections.reverse(temp);
                for (Node n : temp) {
                    System.out.print(n.value);
                }
                flag = !flag;
            }
            System.out.println();
        }
        System.out.println();
    }

    //根据二叉树的前序遍历和中序遍历构建二叉树
    class Solution {
        public Node buildTree(int[] preorder, int[] inorder) {
            if (preorder == null || inorder == null || preorder.length == 0 || inorder.length == 0 || preorder.length != inorder.length)
                return null;
            return buildTreeHelper(preorder,0,preorder.length-1,inorder,0,inorder.length-1);

        }
        public  Node buildTreeHelper(int[] preorder , int pstart , int pend , int[] inorder , int istart, int iend){
            if(pstart > pend || istart > iend) return null;
            Node root = new Node(preorder[pstart]);
            int index = 0;
            while(inorder[istart + index] != preorder[pstart]){
                index++;
            }
            //找准左右字节点对应的数组下标
            root.left = buildTreeHelper(preorder,pstart+1,pstart+index,inorder,istart,istart +index-1);
            root.right = buildTreeHelper(preorder,pstart + index + 1 , pend ,inorder , istart + index + 1 , iend);
            return root;
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值