基础算法集训第18天:深度优先搜索

目录

排列序数

十位数宝藏代码

带分数

[LCR 175. 计算二叉树的深度 - 力扣(LeetCode)](#LCR 175. 计算二叉树的深度 - 力扣(LeetCode))

[104. 二叉树的最大深度 - 力扣(LeetCode)](#104. 二叉树的最大深度 - 力扣(LeetCode))

[226. 翻转二叉树 - 力扣(LeetCode)](#226. 翻转二叉树 - 力扣(LeetCode))

[938. 二叉搜索树的范围和 - 力扣(LeetCode)](#938. 二叉搜索树的范围和 - 力扣(LeetCode))

[LCP 44. 开幕式焰火 - 力扣(LeetCode)](#LCP 44. 开幕式焰火 - 力扣(LeetCode))

[100. 相同的树 - 力扣(LeetCode)](#100. 相同的树 - 力扣(LeetCode))


排列序数

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
string s;
int res;
int n;
int dis[11];
int a[11];
bool vis[11];
void dfs(int x)
{
    if (x == n)
    {
        for (int i = 0; i < n; i++)
        {
            if (a[i] < dis[i])
            {
                res++;
                return;
            }
            else if (a[i] > dis[i])
            {
                return;
            }
        }
        cout << res;
        exit(0);
    }
    for (int i = 0; i < n; i++)
    {
        if (!vis[i])
        {
            vis[i] = true;
            a[x] = i;
            dfs(x + 1);
            a[x] = 0;
            vis[i] = false;
        }
    }
}
int main()
{
    cin >> s;
    n = s.size();
    for (int i = 0; i < n; i++)
    {
        dis[i] = s[i] - 'a';
    }
    dfs(0);
    return 0;
}

十位数宝藏代码

cpp 复制代码
#include <iostream>
#include <algorithm>
using namespace std;
#define int long long
int a[11];
bool vis[11];
int res1 = 0;
int res2 = 1e12;
int n = 10;
void dfs(int x)
{
    if (x == n)
    {
        if (a[0] == 0)
        {
            return;
        }
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            sum *= 10;
            sum += a[i];
        }
        if (sum % 11 == 0)
        {
            res1 = max(res1, sum);
            res2 = min(res2, sum);
        }
        return;
    }
    for (int i = 0; i < n; i++)
    {
        if (!vis[i])
        {
            vis[i] = true;
            a[x] = i;
            dfs(x + 1);
            a[x] = 0;
            vis[i] = false;
        }
    }
}
signed main()
{
    dfs(0);
    cout << res1 - res2;
    return 0;
}

带分数

cpp 复制代码
#include <iostream>
using namespace std;
int a[10];
bool vis[10];
int res;
int n = 9;
int num;
void dfs(int x)
{
    if (x == n)
    {
        for (int i = 0; i < 7; i++)
        {
            for (int j = i + 1; j < 8; j++)
            {
                int A = 0;
                int B = 0;
                int C = 0;
                for (int k = 0; k <= i; k++)
                {
                    A = A * 10 + a[k];
                }
                if (A >= num)
                {
                    continue;
                }
                for (int k = i + 1; k <= j; k++)
                {
                    B = B * 10 + a[k];
                }
                for (int k = j + 1; k < 9; k++)
                {
                    C = C * 10 + a[k];
                }
                if (B % C == 0)
                {
                    if (A + B / C == num)
                    {
                        res++;
                    }
                }
            }
        }
        return;
    }
    for (int i = 1; i <= 9; i++)
    {
        if (!vis[i])
        {
            vis[i] = true;
            a[x] = i;
            dfs(x + 1);
            a[x] = 0;
            vis[i] = false;
        }
    }
}
int main()
{
    cin >> num;
    dfs(0);
    cout << res;
    return 0;
}

LCR 175. 计算二叉树的深度 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int calculateDepth(TreeNode* root) {
        if(root==0)return 0;
        int left=calculateDepth(root->left);
        int right=calculateDepth(root->right);
        return max(left,right)+1;
    }
};

104. 二叉树的最大深度 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==0)return 0;
        int left=maxDepth(root->left);
        int right=maxDepth(root->right);
        return max(left,right)+1;
    }
};

226. 翻转二叉树 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root){
            swap(root->left,root->right);
            invertTree(root->left);
            invertTree(root->right);
        }
        return root;
    }
};

938. 二叉搜索树的范围和 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high) {
        if(root==0)return 0;
        int res=0;
        if(root->val>=low&&root->val<=high)res+=root->val;
        res+=rangeSumBST(root->left,low,high);
        res+=rangeSumBST(root->right,low,high);
        return res;
    }
};

LCP 44. 开幕式焰火 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    unordered_map<int, int> map;
public:
    int numColor(TreeNode* root) {
        if (root == 0)return 0;
        map[root->val]++;
        numColor(root->left);
        numColor(root->right);
        return map.size();
    }
};

100. 相同的树 - 力扣(LeetCode)

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if(p==nullptr&&q==nullptr){
            return true;
        }
        else if(p==nullptr||q==nullptr){
            return false;
        }
        if(p->val!=q->val){
            return false;
        }
        return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    }
};

知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具

相关推荐
Wect6 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP18 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub1 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP2 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP2 天前
一文搞懂激活函数!
算法·面试