代码随想录算法训练营第三十七天| 738.单调递增的数字 968.监控二叉树

题目链接:738.单调递增的数字

class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string strNum=to_string(n);
        int flag=strNum.size();
        for(int i=strNum.size()-1;i>0;i--)
        {
            if(strNum[i-1]>strNum[i])
            {
                flag=i;
                strNum[i-1]--;
            }
        }
        for(int i=flag;i

题目链接:968.监控二叉树

/**
 * 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 {
private:
int result;
int traversal(TreeNode* cur)
{
    if(cur==nullptr) return 2;
    int left=traversal(cur->left);
    int right=traversal(cur->right);
    if(left==2&&right==2) return 0;
    if(left==0||right==0)
    {
        result++;
        return 1;
    }
    if(left==1||right==1)
    {
        return 2;
    }
    return -1;
}
public:
    int minCameraCover(TreeNode* root) {
        result=0;
        if(traversal(root)==0) result++;
        return result;
    }
};

你可能感兴趣的:(代码随想录,算法,leetcode,c++)