帮你从算法的角度来认识二叉树---(一)

一、基础概念

1.定义

二叉树是每个节点最多有两个子节点的树形结构,两个子节点分别叫:左子节点、右子节点

2. 核心术语

根节点:树最顶端的节点

叶子节点:没有子节点的节点

父节点 / 子节点:上下级关系节点

深度:从根到当前节点的层数

高度:从当前节点到最远叶子的层数

3. 特殊二叉树

满二叉树:所有层都填满节点

完全二叉树:除最后一层外都填满,最后一层靠左排列

二叉搜索树 (BST):左 < 根 < 右,中序遍历是升序

平衡二叉树:左右子树高度差 ≤1

二、定义二叉树节点

这里用java来实现

java 复制代码
// 二叉树节点定义
public class TreeNode {
    int val;        // 节点值
    TreeNode left;   // 左子节点
    TreeNode right;  // 右子节点

    // 构造方法
    TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

三、遍历二叉树

1.前序遍历:根-->左-->右

java 复制代码
//先序遍历
public void preOrder(TreeNode root){
    if(root==null) return;
    System.out.print(root.val+" ");//根
    preOrder(root.left);//左
    preOrder(root.right);//右
}

2.中序遍历:左-->根-->右

在上面提到的二叉搜索树是左子树值 < 根节点值 < 右子树值,因此二叉搜索树中序遍历的结果是升序的

java 复制代码
// 中序遍历
public void inOrder(TreeNode root) {
    if (root == null) return;
    inOrder(root.left);                // 左
    System.out.print(root.val + " ");  // 根
    inOrder(root.right);               // 右
}

3.后序遍历:左-->右-->根

java 复制代码
// 后序遍历
public void postOrder(TreeNode root) {
    if (root == null) return;
    postOrder(root.left);              // 左
    postOrder(root.right);             // 右
    System.out.print(root.val + " ");  // 根
}

4.层序遍历:按层遍历(BFS)

用队列实现,这样可以一层一层来访问

java 复制代码
// 层序遍历(队列实现)
public void levelOrder(TreeNode root) {
    if (root == null) return;        // 1. 空树直接返回
    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);              // 2. 根节点入队

    while (!queue.isEmpty()) {       // 3. 队列不为空就继续
        TreeNode node = queue.poll();// 4. 出队一个节点
        System.out.print(node.val + " "); // 5. 访问它

        // 6. 左孩子、右孩子依次入队
        if (node.left != null) queue.offer(node.left);
        if (node.right != null) queue.offer(node.right);
    }
}

四、构建二叉树

java 复制代码
public class BinaryTreeTest {
    public static void main(String[] args) {
        // 构建如下二叉树:
        //        1
        //       / \
        //      2   3
        //     / \
        //    4   5

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
    }
}

五、求二叉树的高度

这里用递归来实现,不断地求左子树的高度和右子树的高度,然后返回两者最大值加1(1是根节点)

java 复制代码
public int getHeight(TreeNode root) {
    if (root == null) return 0;
    int leftH = getHeight(root.left);
    int rightH = getHeight(root.right);
    return Math.max(leftH, rightH) + 1;
}

六、综合测试代码

java 复制代码
import java.util.LinkedList;
import java.util.Queue;

// 节点类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

// 二叉树操作类
public class BinaryTree {
    // 前序
    public void preOrder(TreeNode root) {
        if(root==null)return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    // 中序
    public void inOrder(TreeNode root) {
        if(root==null)return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    // 后序
    public void postOrder(TreeNode root) {
        if(root==null)return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    // 层序
    public void levelOrder(TreeNode root) {
        if(root==null)return;
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            TreeNode n=q.poll();
            System.out.print(n.val+" ");
            if(n.left!=null)q.offer(n.left);
            if(n.right!=null)q.offer(n.right);
        }
    }
    // 树高
    public int getHeight(TreeNode root) {
        if(root==null)return 0;
        return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }

    public static void main(String[] args) {
        BinaryTree tree=new BinaryTree();
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(5);

        System.out.println("前序:");tree.preOrder(root);
        System.out.println("\n中序:");tree.inOrder(root);
        System.out.println("\n后序:");tree.postOrder(root);
        System.out.println("\n层序:");tree.levelOrder(root);
        System.out.println("\n树高:"+tree.getHeight(root));
    }
}

七、小舟有话说

这里仅介绍了一些概念和基本算法,下一篇会着重讲一些常见的算法题哦~

如果喜欢的话可以点点关注,下次找我不迷路~

相关推荐
Kk.08022 小时前
数据结构|排序算法(三)堆排序
java·数据结构·排序算法
龙文浩_2 小时前
AI深度学习演进之路:从机器学习到大模型的范式变革
人工智能·深度学习·神经网络·算法·回归·线性回归
LTphy2 小时前
P3131 [USACO16JAN] Subsequences Summing to Sevens S
算法·前缀和·蓝桥杯
cpp_25012 小时前
P1569 [USACO ?] Generic Cow Protests【来源请求】
数据结构·c++·算法·题解·洛谷·线性dp
繁星星繁2 小时前
Docker(一)
java·c语言·数据结构·c++·docker·容器·eureka
青稞社区.2 小时前
大模型RL算法梳理:从全量词元到部分词元的路径演化
算法
qiqsevenqiqiqiqi2 小时前
一维dp知识点
算法·动态规划
ZHANG13HAO2 小时前
蚁群算法(蚁聚算法)深度解析与 mTSP 实战:物流多车协同配送优化
人工智能·算法·机器学习
D_C_tyu2 小时前
HTML | 基于权重评估算法实现自动游戏功能的俄罗斯方块小游戏
算法·游戏·html