【刷题】DFS

DFS

递归:
1.判断是否失败终止
2.判断是否成功终止,如果成功的,记录一个成果
3.遍历各种选择,在这部分可以进行剪枝
4.在每种情况下进行DFS,并进行回退。

199. 二叉树的右视图

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1:
在这里插入图片描述
输入: [1,2,3,null,5,null,4]
输出: [1,3,4]
示例 2:
输入: [1,null,3]
输出: [1,3]
示例 3:
输入: []
输出: []

class Solution {
    
    
public:
    vector<int> rightSideView(TreeNode* root) {
    
    
        unordered_map<int, int> rightmostValueAtDepth;
        int max_depth = -1;

        stack<TreeNode*> nodeStack;
        stack<int> depthStack;
        nodeStack.push(root);
        depthStack.push(0);

        while (!nodeStack.empty()) {
    
    
            TreeNode* node = nodeStack.top();nodeStack.pop();
            int depth = depthStack.top();depthStack.pop();

            if (node != NULL) {
    
    
            	// 维护二叉树的最大深度
                max_depth = max(max_depth, depth);

                // 如果不存在对应深度的节点我们才插入
                if (rightmostValueAtDepth.find(depth) == rightmostValueAtDepth.end()) {
    
    
                    rightmostValueAtDepth[depth] =  node -> val;
                }

                nodeStack.push(node -> left);
                nodeStack.push(node -> right);
                depthStack.push(depth + 1);
                depthStack.push(depth + 1);
            }
        }

        vector<int> rightView;
        for (int depth = 0; depth <= max_depth; ++depth) {
    
    
            rightView.push_back(rightmostValueAtDepth[depth]);
        }

        return rightView;
    }
};

39. 组合总和

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。
对于给定的输入,保证和为 target 的不同组合数少于 150 个。
示例 1:
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
2 和 3 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
示例 2:
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
示例 3:
输入: candidates = [2], target = 1
输出: []

class Solution {
    
    
public:
    void dfs(vector<int>& candidates, int target, vector<vector<int>>& ans, vector<int>& combine, int index) {
    
    
        if (index >= candidates.size()) return;
        if (target==0) {
    
    
            ans.emplace_back(combine);
            return;
        }
        dfs(candidates, target, ans, combine, index+1);
        if (candidates[index]<=target){
    
    
            combine.push_back(candidates[index]);
            dfs(candidates, target-candidates[index], ans, combine, index);
            combine.pop_back();
        }
    }

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
    
    
        vector<vector<int>> ans;
        vector<int> combine;
        dfs(candidates, target, ans, combine, 0);
        return ans;
    }
};

2477. 到达首都的最少油耗

给你一棵 n 个节点的树(一个无向、连通、无环图),每个节点表示一个城市,编号从 0 到 n - 1 ,且恰好有 n - 1 条路。0 是首都。给你一个二维整数数组 roads ,其中 roads[i] = [ai, bi] ,表示城市 ai 和 bi 之间有一条 双向路 。

每个城市里有一个代表,他们都要去首都参加一个会议。

每座城市里有一辆车。给你一个整数 seats 表示每辆车里面座位的数目。

城市里的代表可以选择乘坐所在城市的车,或者乘坐其他城市的车。相邻城市之间一辆车的油耗是一升汽油。

请你返回到达首都最少需要多少升汽油。
示例 1:
在这里插入图片描述
输入:roads = [[0,1],[0,2],[0,3]], seats = 5
输出:3
解释:

  • 代表 1 直接到达首都,消耗 1 升汽油。
  • 代表 2 直接到达首都,消耗 1 升汽油。
  • 代表 3 直接到达首都,消耗 1 升汽油。
    最少消耗 3 升汽油。
    示例 2:
    在这里插入图片描述
    输入:roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
    输出:7
    解释:
  • 代表 2 到达城市 3 ,消耗 1 升汽油。
  • 代表 2 和代表 3 一起到达城市 1 ,消耗 1 升汽油。
  • 代表 2 和代表 3 一起到达首都,消耗 1 升汽油。
  • 代表 1 直接到达首都,消耗 1 升汽油。
  • 代表 5 直接到达首都,消耗 1 升汽油。
  • 代表 6 到达城市 4 ,消耗 1 升汽油。
  • 代表 4 和代表 6 一起到达首都,消耗 1 升汽油。
    最少消耗 7 升汽油。
    示例 3:
    在这里插入图片描述
    输入:roads = [], seats = 1
    输出:0
    解释:没有代表需要从别的城市到达首都。
class Solution {
    
    
public:
    long long minimumFuelCost(vector<vector<int>>& roads, int seats) {
    
    
        int n = roads.size();
        vector<vector<int>> g(n+1);
        for (auto &e : roads) {
    
    
            g[e[0]].push_back(e[1]);
            g[e[1]].push_back(e[0]);
        }
        long long res = 0;
        function<int(int, int)> dfs = [&](int cur, int fa) -> int {
    
    
            int peopleSum = 1;
            for (auto ne : g[cur]) {
    
    
                if (ne != fa) {
    
    
                    int peopleCnt = dfs(ne, cur);
                    peopleSum += peopleCnt;
                    res += (peopleCnt + seats - 1) / seats;
                }
            }
            return peopleSum;
        };

        dfs(0, -1);
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_42725437/article/details/134700722
今日推荐