二叉树code

二叉树遍历

import java.util.Stack;

/**
 * @ author:mian
 * @ DATA:2018/5/7 16:10
 */
class Node{
    public int value;
    public Node left;
    public Node right;
    public Node(int data){
        this.value = data;
    }
}
public class 二叉树遍历 {
    //前序遍历-递归
    public static void preOrderRecur(Node head){
        if(head==null){
            return;
        }
        System.out.println(head.value+" ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }
    //中序遍历
    public static void inOrderRecur(Node head){
        if(head==null){
            return;
        }
        inOrderRecur(head.left);
        System.out.println(head.value+"");
        inOrderRecur(head.right);
    }
    //后序遍历
    public static void posOrderRecur(Node head){
        if(head==null){
            return;
        }
        posOrderRecur(head.left);
        posOrderRecur(head.right);
        System.out.println(head.value+" ");
    }
    //前序非递归
    public static void preOrderUnRecur(Node head){
        System.out.println("pre-order:");
        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 inOrderUnRecur(Node head){
        System.out.println("in-order");
        if(head!=null){
            Stack<Node> stack=new Stack<Node>();
            if(head!=null){
                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();
    }
}

前序,中序,后序任选两个重构二叉树

import java.util.HashMap;

/**
 * @ author:mian
 * @ DATA:2018/5/7 16:51
 */
public class 遍历序列重构二叉树 {
    //先序和中序重构二叉树
    public static Node preInToTree(int[] pre,int[] in){
        if(pre==null||in==null){
            return null;
        }
        HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
        for(int i=0;i<in.length;i++){
            map.put(in[i],i);
        }
        return preIn(pre,0,pre.length-1,in,0,in.length-1,map);
    }
    public static Node preIn(int[] p,int pi,int pj,int[] n,int ni,int nj,HashMap<Integer,Integer> map){
        if(pi>pj){
            return null;
        }
        Node head = new Node(p[pi]);
        int index = map.get(p[pi]);
        head.left = preIn(p,pi+1,pi+index-ni,n,ni,index-1,map);
        head.right =preIn(p,pi+index-ni,pj,n,index+1,nj,map);
        return head;
    }

    //中序和后序
    public static Node inPosToTree(int[] in,int[] pos){
        if(in==null||pos==null){
            return null;
        }
        HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
        for(int i=0;i<in.length;i++){
            map.put(in[i],i);
        }
        return inPos(in,0,in.length-1,pos,0,pos.length-1,map);
    }
    public static Node inPos(int[] n,int ni,int nj,int[] s,int si,int sj,HashMap<Integer,Integer> map){
        if(si>sj){
            return null;
        }
        Node head=new Node(s[sj]);
        int index = map.get(s[sj]);
        head.left = inPos(n,ni,index-1,s,si,si+index-ni-1,map);
        head.right=inPos(n,index+1,nj,s,si+index-ni,sj-1,map);
        return head;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,489评论 1 31
  • 前言 树是数据结构中的重中之重,尤其以各类二叉树为学习的难点。一直以来,对于树的掌握都是模棱两可的状态,现在希望通...
    MrHorse1992阅读 354,007评论 51 536
  • 数据结构和算法--二叉树的实现 几种二叉树 1、二叉树 和普通的树相比,二叉树有如下特点: 每个结点最多只有两棵子...
    sunhaiyu阅读 6,530评论 0 14
  • 编程中我们会遇到多少挫折?表放弃,沙漠尽头必是绿洲。 学习二叉树的意义 由于二叉树的知识更倾向于理论,所以我们在实...
    神经骚栋阅读 6,308评论 5 57
  • 树和二叉树 1、树的定义 树(Tree)是由一个 或 多个结点 组成的有限集合T,且满足: ①有且仅有一个称为根的...
    利伊奥克儿阅读 1,398评论 0 1