Java实现二叉树(简单版)

1.先定义节点

java 复制代码
/*
  定义一个树节点
 */
public class TreeNode {
    int val;         //存储值
    TreeNode left;   //左子树
    TreeNode right;  //右子树

    //无参构造方法
    TreeNode (){

    }

    //有参构造方法
    TreeNode(int val){
        this.val=val;
    }

    TreeNode(int val,TreeNode left,TreeNode right){
        this.val=val;
        this.left=left;
        this.right=right;
    }
}

2.手动创建一个二叉树并遍历。

此处同时使用了前序遍历、中序遍历、后序遍历,实际只使用一种方法即可。

java 复制代码
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //手动创建一个树
        TreeNode root=createTree();

        //1.前序遍历
        List<Integer> result=preorderTraversal(root);

        //2.中序遍历
        List<Integer> result2=inorderTraversal(root);

        //3.后序遍历
        List<Integer> result3=postorderTraversal(root);

        System.out.println(result);
        System.out.println(result2);
        System.out.println(result3);
    }


    //手动创建二叉树
    public static TreeNode createTree(){
        TreeNode root=new TreeNode(1);  //创建根节点
        TreeNode treeNode2=new TreeNode(2);
        TreeNode treeNode3=new TreeNode(3);
        TreeNode treeNode4=new TreeNode(4);
        TreeNode treeNode5=new TreeNode(5);
        TreeNode treeNode6=new TreeNode(6);
        TreeNode treeNode7=new TreeNode(7);

        root.left = treeNode2;
        root.right = treeNode3;
        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;
        treeNode3.left = treeNode6;
        treeNode3.right = treeNode7;

        return root;
    }

    //前序遍历
    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        preorder(root,result);
        return result;
    }

    public static void preorder(TreeNode root,List<Integer> result){
        if(root==null) return;    //如果当前节点为空,结束递归
        result.add(root.val);    //不为空,则将当前节点的值,加入链表中
        preorder(root.left,result);
        preorder(root.right,result);
    }

    //中序遍历
    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        inorder(root,result);
        return result;
    }
    public static void inorder(TreeNode root,List<Integer> result){
        if(root==null) return;
        inorder(root.left,result);
        result.add(root.val);
        inorder(root.right,result);
    }

    //后序遍历
    public static List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        postorder(root,result);
        return result;
    }
    public static void postorder(TreeNode root,List<Integer> result){
        if(root==null) return;
        postorder(root.left,result);
        postorder(root.right,result);
        result.add(root.val);
    }
}
相关推荐
njsgcs几秒前
solidworks自动标注折弯4 无向图 c#
开发语言·c#·solidworks
染夕陌木4 分钟前
RPC/服务调用框架中“方法无法应用到给定类型”错误的通用排查指南
java·ide·rpc
地球资源数据云4 分钟前
1900-2023年中国物种分布点位矢量数据集
大数据·数据结构·数据库·数据仓库·人工智能
大大杰哥7 分钟前
String常用方法
java
楼兰公子13 分钟前
读取rpi摄像头
linux·服务器·算法
c++之路13 分钟前
C++ 多线程
开发语言·c++
渡之15 分钟前
NaviLoc - GNSS 拒止环境下无人机空对地卫星视觉定位算法 论文整理
算法·无人机·飞控
CHANG_THE_WORLD19 分钟前
<Fluent Python > Unicode 文本与字节
开发语言·python
AI人工智能+电脑小能手24 分钟前
【大白话说Java面试题】【Java基础篇】第20题:HashMap在计算index的时候,为什么要对数组长度做减1操作
java·开发语言·数据结构·后端·面试·哈希算法·hash-index
凯瑟琳.奥古斯特25 分钟前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html