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

一、基础概念

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));
    }
}

七、小舟有话说

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

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

相关推荐
m0_639906941 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
ZhengEnCi2 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
秋天的怀念_David3 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi3 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6183 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang3 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
2601_970703835 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你02145 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
ZhengEnCi6 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
Flynn Liang6 小时前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai