CSNotesCSNotes
TODO
LeetCode
数据结构
计算机组成原理
操作系统
计算机网络
数据库
Java
SSM
React
实用工具
GitHub
TODO
LeetCode
数据结构
计算机组成原理
操作系统
计算机网络
数据库
Java
SSM
React
实用工具
GitHub
  • 第一章 绪论

    • 1.1 数据结构的基本概念
  • 第二章 线性表

    • 2.1 线性表的定义和基本操作
    • 2.2 线性表的顺序表示
    • 2.3 线性表的链式表示
  • 第三章 栈、队列和数组

    • 3.1 栈
    • 3.2 队列
    • 3.3 栈和队列的应用
    • 3.4 数组和特殊矩阵
  • 第四章 串

    • 4.1 串的定义和实现
    • 4.2 串的模式匹配
  • 第五章 树与二叉树

    • 5.1 树的基本概念
    • 5.2 二叉树的概念
    • 5.3 二叉树的遍历和线索二叉树
    • 5.4 树、森林
    • 5.5 树和二叉树的应用
  • 第六章 图

    • 6.1 图的基本概念
    • 6.2 图的存储及基本操作
    • 6.3 图的遍历
    • 6.4 图的应用
  • 第七章 查找

    • 7.1 查找的基本概念
    • 7.2 顺序查找和折半查找
    • 7.3 树型查找
    • 7.4 B 树和 B+ 树
    • 7.5 散列表 (哈希表)
  • 第八章 排序

    • 8.1 排序的基本概念
    • 8.2 插入排序
    • 8.3 交换排序
    • 8.4 选择排序
    • 8.5 归并排序和基数排序
    • 8.6 外部排序
  • 第九章 贪心算法

    • 9.1 贪心算法
  • 第十章 动态规划

    • 10.1 动态规划

5.3 二叉树的遍历和线索二叉树

5.3.1 二叉树的遍历

先序遍历

void preorder(TreeNode *root, vector<int> &res)
    {
        if (!root)
        {
            return;
        }
        res.push_back(root->val);
        preorder(root->left, res);
        preorder(root->right, res);
    }
    vector<int> preorderTraversal(TreeNode *root)
    {
        vector<int> res;
        preorder(root, res);
        return res;
    }

中序遍历

void inorder(TreeNode *root, vector<int> &res)
    {
        if (!root)
        {
            return;
        }
        inorder(root->left, res);
        res.push_back(root->val);
        inorder(root->right, res);
    };
    vector<int> inorderTraversal(TreeNode *root)
    {
        vector<int> res;
        inorder(root, res);
        return res;
    }

后序遍历

void postorder(TreeNode *root, vector<int> &res)
    {
        if (!root)
        {
            return;
        }
        postorder(root->left, res);
        postorder(root->right, res);
        res.push_back(root->val);
    }
    vector<int> postorderTraversal(TreeNode *root)
    {
        vector<int> res;
        postorder(root, res);
        return res;
    }

层次遍历

队头出队,孩子入队

由遍历序列构造二叉树

由(先序遍历、后序遍历、层次遍历)三选一,加上中序遍历可以唯一确定一棵二叉树。

5.3.2 线索二叉树

加快查找结点前驱和后继的速度。

树森林二叉树
先根遍历先序遍历先序遍历
后根遍历中序遍历中序遍历
编辑此页
上次更新:
Prev
5.2 二叉树的概念
Next
5.4 树、森林