二叉树的构建及递归遍历

2023-05-16

定义结构体

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

    public Node(int value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}


/**
*            二叉树结构:
*                     1
*                   /   \
*                  2     3
*                 /     / \
*                4     5   6
*                 \        /
*                 7        8
*/

构建二叉树并递归进行前序、中序、后序遍历

注意:构建二叉树时,是由下往上构建的,即先构建子节点,再构建父节点。


public class binaryTree {
   public static Node initTree(){
       Node n7 = new Node(7,null,null);
       Node n8 = new Node(8,null,null);
       Node n4 = new Node(4,null,n7);
       Node n5 = new Node(5,null,null);
       Node n6 = new Node(6,n8,null);
       Node n2 = new Node(2,n4,null);
       Node n3 = new Node(3,n5,n6);
       Node n1 = new Node(1,n2,n3);
       return n1;

   }
    //前序遍历
   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;
       }else{
           inOrderRecur(head.left);
           System.out.print(head.value+" ");
           inOrderRecur(head.right);
       }
    }

    //后序遍历
    public static void postOrderRecur(Node head){
       if (head == null){
           return;
       }else{
           postOrderRecur(head.left);
           postOrderRecur(head.right);
           System.out.print(head.value+" ");
       }
    }
    public static void main(String[] args) {
        Node node = binaryTree.initTree();
        System.out.print("先序遍历:");
        binaryTree.preOrderRecur(node);
        System.out.println();
        System.out.print("中序遍历:");
        binaryTree.inOrderRecur(node);
        System.out.println();
        System.out.print("后序遍历:");
        binaryTree.postOrderRecur(node);
    }
}
/**
*先序遍历:1 2 4 7 3 5 6 8 
*中序遍历:4 7 2 1 5 3 8 6 
*后序遍历:7 4 2 5 8 6 3 1 
*/

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

二叉树的构建及递归遍历 的相关文章

随机推荐

  • 使用 SPDK 技术优化虚拟机本地存储的 IO 性能

    SPDK Storage performance development kit 是由 Intel 发起 xff0c 用于使用 NVMe SSD 作为后端存储的应用软件加速库 该软件库的核心是实现了用户态 异步 无锁 轮询方式的 NVMe
  • 【nest.js_01】nest.js初识-项目初始化

    记录学习 nest js 开发的过程 演示环境 Windows 10 mysql v8 0 12 Redis3 v 2 100 node v14 16 1 npm v6 14 12 nestjs v8 1 1 链接 node 下载安装地址
  • 树莓派系统安装及使用(详细步骤)

    1 安装系统 1 1 下载系统镜像 下载地址https www raspberrypi org downloads 可以看到有 NOOBS和Raspbian xff08 1 xff09 NOOBS为傻瓜式安装 xff0c 使用NOOBS安装
  • VS断点调试技巧

    条件断点 xff1a 条件断点就是当满足某种条件时才会触发的断点 例如在循环体中 xff0c 我们想查看第一万次循环的结果 xff0c 显然不能一步一步运行程序 xff0c 而应当在断点处设置条件 使用流程 xff1a 1 首先需要打一个断
  • 使用VS远程连接linux并进行开发

    此功能需要vs2015及以上 xff0c 此处以VS2019为例 在安装VS时需要把linux支持选上 xff0c 已经安装了的可以通过修改VS进行安装 具体流程 xff1a 首先新建项目 选择Linux的控制台应用程序 选择好位置 名称后
  • OpenCV轮廓相关操作 C++

    参考 参考 轮廓的基本概念 在OpenCV中 xff0c 可以通过cv findContours 函数 xff0c 在灰度图中寻找轮廓 函数原型 xff1a span class token keyword void span span c
  • OpenCV绘图相关操作 C++

    绘制相关知识 lineType线条风格介绍 opencv的线条风格由枚举值描述 xff1a span class token comment type of line span span class token keyword enum s
  • C++单例模式的几种实现

    单例模式 Singleton Pattern 的概念 模式定义 保证一个类仅有一个实例 xff0c 并提供一个访问它的全局访问点 饿汉单例和懒汉单例 常见的单例模式有两个分支 xff0c 饿汉单例和懒汉单例 饿汉单例是指在程序初始化时就把单
  • OpenCV深度图转点云

    需要先进行畸变校正 xff0c 再通过深度图转点云 相机的相关参数需要事先通过标定获得 span class token macro property span class token directive hash span span cl
  • 贝叶斯分类器,什么是朴素贝叶斯,后续为使用贝叶斯实现海量数据的邮件筛选。带源码数据集和解决思路

    朴素贝叶斯分类器 任务 xff1a 理解朴素贝叶斯分类器 实现我们的第一个贝叶斯分类器 使用朴素贝叶斯分类器分类邮件 概率论基础 xff1a 随机变量 xff1a 这个变量的值依赖于概率 抛硬币 xff08 其结果可能是正面 xff0c 也
  • [jetson]jetpack5.1的ubuntu20.04更换为清华源

    为使后续下载安装顺利 xff0c 需要给 Ubuntu 系统进行换源 Ctrl 43 Alt 43 T 打开终端 xff0c 输入如下命令 sudo gedit etc apt sources list 删除原文件内容 xff0c 添加如下
  • Windows系统借助WSL2可使用Linux系统开发

    一 起源 Windows 10 2004 发布后 xff0c WSL2 也可以在正式版 Windows 10 中使用 xff0c 相比于 macOS xff0c WSL2 是一个原生 Linux 环境而非类 unix 环境 xff0c 甚至
  • wiremock基本使用

    进入wiremock官网 xff0c 选择stand alone xff0c 下载jar包 http wiremock org docs running standalone 运行该jar包 xff0c 并设置端口号 xff0c 如 xff
  • 建行笔试题,最少补给品问题

    import java util Scanner public class Main public static void main String args Scanner sc 61 new Scanner System in Strin
  • springboot传递参数并选择激活环境运行jar包

    java jar spring boot demo 0 0 1 SNAPSHOT jar SOME ENV 61 always spring profiles active 61 prod
  • springcloud alibaba

    springcloud alibaba 版本说明 https github com alibaba spring cloud alibaba wiki E7 89 88 E6 9C AC E8 AF B4 E6 98 8E E6 AF 95
  • @JSONField的一些使用基础

    https blog csdn net dmw412724 article details 93761161
  • MyBatis Mapper 传递多个参数

    在pojo类对应的映射文件中 xff0c 对应的参数类型可以省略 传递方式 1 接口正常书写 xff0c 映射文件中SQL语句的占位符必须用 arg0 agr1 或param1 param2 接口 xff1a public Customer
  • MyBatis一对多的左连接查询、分步查询以及插入和删除操作

    例如有两张表 xff0c 分别是客户表和订单表 xff0c 一个客户有多个订单 xff0c 一个订单属于一个客户 两个实体类Customer Order 如下 xff1a package com itlike domain import l
  • 二叉树的构建及递归遍历

    定义结构体 public class Node int value Node left Node right public Node int value Node left Node right this value 61 value th