Leetcode 111-120

LeetCode 111. 二叉树的最小深度

分析

递归分析左右子树.
根据定义, 如果u是叶子结点, 那么深度为1.
如果不是叶子节点, 分成3种情况
1.左子树a, 右子树b 不空, 返回min(f(a), f(b)) + 1
2.a不空, b空, 返回f(a) + 1
3.a空, b不空 返回f(b) + 1
在这里插入图片描述

代码

/**
 * 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 minDepth(TreeNode* root) {
    
    
        if (!root) return 0;
        if (!root->left && !root->right) return 1; // 如果是叶子节点 返回1
        if (root->left && root->right) return min(minDepth(root->left), minDepth(root->right)) + 1;
        if (root->left) return minDepth(root->left) + 1;
        return minDepth(root->right) + 1;
    }
};

LeetCode 112. 路径总和

分析

自上而下递归, 然后判断叶子结点的sum是否==0
在这里插入图片描述

代码

/**
 * 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 hasPathSum(TreeNode* root, int sum) {
    
    
        if (!root) return false;
        sum -= root->val;
        if (!root->left && !root->right) return !sum;
        return root->left && hasPathSum(root->left, sum) || root->right && hasPathSum(root->right, sum);

    }
};

LeetCode 113. 路径总和 II

分析

新建一个dfs函数去递归, 不要在原函数中递归
还要新建全局变量res, path

代码

/**
 * 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:
    vector<vector<int>> res;
    vector<int> path;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
    
    
        if (root) dfs(root, sum);
        return res;
    }
    void dfs(TreeNode* root, int sum){
    
    
        path.push_back(root->val);
        sum -= root->val;
        if (!root->left && !root->right){
    
    
            if (!sum) res.push_back(path);
        }else{
    
    
            if (root->left) dfs(root->left, sum);
            if (root->right) dfs(root->right, sum);
        }
        path.pop_back();
    }
};

LeetCode 114. 二叉树展开为链表

分析

在这里插入图片描述
在这里插入图片描述

代码

/**
 * 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:
    void flatten(TreeNode* root) {
    
    
        while (root){
    
    
            auto p = root->left;
            if (p){
    
    
                while (p->right) p = p->right;
                p->right = root->right;
                root->right = root->left;
                root->left = NULL;
            }
            root = root->right;
        }   
    }
};

LeetCode 115. 不同的子序列

分析

闫氏dp分析法.
f[i][j] 表示s[1~i]所有前i个字符和t前j个字符匹配的子序列
注意 右边是无条件可以选择的情况, 所以f[i][j] = f[i - 1][j]
在这里插入图片描述
中间结果会爆int, 用longlong存储

代码

class Solution {
    
    
public:
    int numDistinct(string s, string t) {
    
    
        int n = s.size(), m = t.size();
        s = ' '  + s, t = ' ' + t;
        vector<vector<long long >> f(n + 1, vector<long long>(m + 1));
        for (int i = 0; i < n; i ++ ) f[i][0] = 1; // 注意i从0开始
        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= m; j ++ ){
    
    
                f[i][j] = f[i - 1][j];
                if (s[i] == t[j]) f[i][j] += f[i - 1][j - 1];
            }
        return f[n][m];
    }
}; 

猜你喜欢

转载自blog.csdn.net/esjiang/article/details/114277688