【LeetCode】HOT 100(22)

Introduction to the question list:

Selected 100 most popular questions on LeetCode, suitable for beginners who are new to algorithms and data structures and those who want to improve efficiently in a short period of time, master these 100 questions, and you already have the ability to learn in code The basic ability to pass through the world.

Table of contents

Introduction to the question list:

Topic: 538. Convert Binary Search Tree to Accumulating Tree - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Title: 494. Goal and - Leetcode

The interface of the topic:

Problem-solving ideas:

code:

It's over! ! ! !

Write at the end:


Topic: 538. Convert Binary Search Tree to Accumulating Tree - Leetcode

The interface of the topic:

/**
 * 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 sum = 0;
    TreeNode* convertBST(TreeNode* root) {

    }
};

Problem-solving ideas:

This question is not difficult,

The most difficult thing is that you can’t understand what the title means. If you understand it, it’s easy to do.

In fact, there are only a few key points in this question,

I list them:

1. This is a binary search tree

2. The nature of the cumulative tree: the value of each node = the value of all nodes larger than him + his own value

In fact, these two properties

And the title is to let us convert this binary search tree into an accumulating tree.

So we only need to start searching from the largest node according to the nature of the binary search tree,

Then add up the values ​​sequentially.

code show as below:

code:

/**
 * 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 sum = 0;
    TreeNode* convertBST(TreeNode* root) {
        if(root == nullptr) return nullptr;
        convertBST(root->right);
        sum += root->val;
        root->val = sum;
        convertBST(root->left);
        return root;
    }
};

It's over! ! ! !

Title: 494. Goal and - Leetcode

The interface of the topic:

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {

    }
};

Problem-solving ideas:

This problem is dynamic programming again.

I originally wanted to use violent enumeration, but it failed, and there are examples that make it difficult

Dynamic programming again, damn it,

I have summer vacation two weeks later, I have time,

Immediately go to the dynamic programming question and get you down,

Now I simply use a search to write,

The search logic is relatively simple, so I won’t explain it here.

Unexpectedly, my code ran for more than a thousand milliseconds and returned to me. . .

code show as below:

code:

class Solution {
public:
    int ans = 0;
    int findTargetSumWays(vector<int>& nums, int target) {
        dfs(nums, target, 0, 0);
        return ans;
    }
private:   
    void dfs(const vector<int>& nums, int target, int start, int sum) {
        if(sum == target && start == nums.size()) {
            ans++;
            return;
        }

        if(start >= nums.size()) return;

        dfs(nums, target, start + 1, sum + nums[start]);
        dfs(nums, target, start + 1, sum - nums[start]);
    }
};

It's over! ! ! !

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131338413