408相关算法刷题【树篇】

LeetCode 94. 二叉树的中序遍历

题目类型

  1. 中序遍历

题目链接

https://leetcode.cn/problems/binary-tree-inorder-traversal

思路

AC代码

class Solution {
public:
    vector<int> res;
    void inorder(TreeNode* root)
    &#123;
        if (!root) return;
        inorder(root->left);
        res.push_back(root->val);
        inorder(root->right);
        return;
    &#125;
    vector<int> inorderTraversal(TreeNode* root) &#123;
        inorder(root);
        return res;
    &#125;
&#125;;

LeetCode 95. 不同的二叉搜索树 II

题目类型

  1. DFS回溯

题目链接

https://leetcode.cn/problems/unique-binary-search-trees-ii/

思路

这道题不看题解的话 我想不出来 呜呜呜

  1. 写一个generate(int l, int r)函数,该函数枚举(l, r)的每一个点i,在(l, i - 1)上构造左子树集合,在(i + 1, r)上构造右子树集合
  2. 然后将左子树集合和右子树集合 与根节点拼接

AC代码

class Solution &#123;
public:
    vector<TreeNode*>  dfs(int l, int r)
    &#123;
        if (l > r) return &#123;nullptr&#125;;
        // 存放结果集合
        vector<TreeNode*> res;

        // 枚举根节点
        for (int i = l; i <= r; i ++)
        &#123;
            vector<TreeNode*> left = dfs(l, i - 1);
            vector<TreeNode*> right = dfs(i + 1, r);

            // 枚举左子树和右子树集合
            // 拼接左子树和右子树
            for (auto l : left)
            &#123;
                for (auto r : right)
                &#123;
                    // 以i为根节点
                    TreeNode* root = new TreeNode(i);
                    root->left = l;
                    root->right = r;
                    res.push_back(root);
                &#125;
            &#125;

        &#125;
        return res;
    &#125;
    vector<TreeNode*> generateTrees(int n) &#123;
        if (n == 0) return &#123;&#125;;
        return dfs(1, n);
    &#125;
&#125;;

LeetCode 95. 不同的二叉搜索树

题目类型

  1. 卡兰特数

题目链接

https://leetcode.cn/problems/unique-binary-search-trees/

思路

AC代码

class Solution &#123;
public:
    int numTrees(int n) &#123;
        long long c = 1;
        for (int i = 0; i < n; i ++)
        &#123;
            c = 2 * c * (2 * i + 1) / (i + 2);
        &#125;
        return (int) c;
    &#125;
&#125;;

LeetCode 98. 验证二叉搜索树

题目类型

  1. 中序遍历

题目链接

https://leetcode.cn/problems/validate-binary-search-tree/submissions/

思路

此题是22年408统考的题,其实408统考题大部分在力扣都能找到
方法1:中序遍历

AC代码 (中序遍历)

class Solution &#123;
public:
    bool isValidBST(TreeNode* root) &#123;
        return dfs(root, LONG_MIN, LONG_MAX);
    &#125;

    bool dfs(TreeNode* root, long long lower, long long upper)
    &#123;
        if (root == nullptr) return true;

        if (root->val <= lower || root->val >= upper) return false;

        return ( dfs(root->left, lower, root->val) && dfs(root->right, root->val, upper));
    &#125;
&#125;;

LeetCode 99. 恢复二叉搜索树

题目类型

  1. 题目链接

https://leetcode.cn/problems/recover-binary-search-tree/

思路

AC代码


##

题目类型

  1. 题目链接

思路

AC代码


##

题目类型

  1. 题目链接

思路

AC代码


##

题目类型

  1. 题目链接

思路

AC代码


LeetCode 102. 验证二叉搜索树

题目类型

  1. 层序遍历

题目链接

https://leetcode.cn/problems/binary-tree-level-order-traversal/

思路

简单的数据结构题,使用队列即可,不同点在于每一层分开输出,在开始访问每一层时,当前队列的长度为该结点个数,记录下来即可

AC代码

class Solution &#123;
public:
    vector<vector<int>> levelOrder(TreeNode* root) &#123;
        queue<TreeNode*> q;
        vector<vector<int>> res;
        if (!root) return res;

        q.push(root);
        while (q.size())
        &#123;
            int currentLevelSize = q.size();
            res.push_back(vector<int> ());
            // 队列中元素个数为当前层的结点个数
            for (int i = 0; i < currentLevelSize; i ++)
            &#123;
                TreeNode* node = q.front(); q.pop();
                res.back().push_back(node->val);

                // 将node结点的左右孩子放入队列中
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            &#125;
        &#125;
        return res;
    &#125;
&#125;;

##

题目类型

  1. 题目链接

思路

AC代码


##

题目类型

  1. 题目链接

思路

AC代码


##【未完待更】


 上一篇
【算法提高篇】搜索 【算法提高篇】搜索
Flood FillAcWing 1097. 池塘计数题目类型 BFS 题目链接 https://www.acwing.com/problem/content/1099/ 思路一 遍历每个方块的“八面图”,注意除去中间,即当前方块 如果
2023-01-15
下一篇 
LeetCode刷题记41-50 LeetCode刷题记41-50
LeetCode 41. 缺失的第一个正数题目类型 哈希表 技巧法 题目链接 https://leetcode.cn/problems/first-missing-positive/ 思路方法一(哈希表): 将 nums 中每个元素放
  目录