Leetcode 15

15. 3Sum(*)

class Solution {
public:
    vector> threeSum(vector& nums) {
        vector> res;
        sort(nums.begin(), nums.end());

        for(int i=0; i 0) return res;
            if(i>0 && nums[i] == nums[i-1]) continue;

            int left = i+1;
            int right = nums.size()-1;
            while(left < right){
                if(nums[i]+nums[left]+nums[right] < 0){
                    left++;
                }else if(nums[i]+nums[left]+nums[right] > 0){
                    right--;
                }else{
                    vector val = {nums[i], nums[left], nums[right]};
                    res.push_back(val);
                    while(left < right && nums[left] == nums[left+1]) left++;
                    while(left < right && nums[right] == nums[right-1]) right--;
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
};

1.确定思路

2.注意相等的时候,要while循环去掉一样的

637. Average of Levels in Binary Tree(OK)

class Solution {
public:
    vector averageOfLevels(TreeNode* root) {
        queue que;
        vector res;

        que.push(root);

        while(!que.empty()){
            int size = que.size();
            double sum = 0;

            for(int i=0; ival;
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
            res.push_back(sum/(double)size);
        }
        return res;
    }
};

102. Binary Tree Level Order Traversal(OK)

class Solution {
public:
    vector> levelOrder(TreeNode* root) {
        queue que;
        vector> res;

        if(root != NULL) que.push(root);

        while(!que.empty()){
            int size = que.size();
            vector levelNode;

            for(int i=0; ival);
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
            res.push_back(levelNode);
        }
        return res;
    }
};

103. Binary Tree Zigzag Level Order Traversal(想一下思路)

class Solution {
public:
    vector> zigzagLevelOrder(TreeNode* root) {
        queue que;
        vector> res;
        bool flag = true;
        if(root != NULL) que.push(root);

        while(!que.empty()){
            int size = que.size();
            vector level;
            
            for(int i=0; ival);
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
            if(!flag) reverse(level.begin(), level.end());
            flag = !flag;
            res.push_back(level);
        }
        return res;
    }
};

530. Minimum Absolute Difference in BST(想思路)

class Solution {
private:
    vector res;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        res.push_back(root->val);
        traversal(root->right);
    }
public:
    int getMinimumDifference(TreeNode* root) {
        int minDif = INT_MAX;
        traversal(root);
        for(int i=1; i

230. Kth Smallest Element in a BST

class Solution {
private:
    vector res;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        res.push_back(root->val);
        traversal(root->right);
    }
public:
    int kthSmallest(TreeNode* root, int k) {
        traversal(root);
        return res[k-1];
    }
};
class Solution {
private:
    int res;
    int count;
    void traversal(TreeNode* root, int k){
        if(root == NULL) return;
        traversal(root->left, k);
        count++;
        if(count == k){
            res = root->val;
            return;
        }
        traversal(root->right, k);
    }
public:
    int kthSmallest(TreeNode* root, int k) {
        traversal(root, k);
        return res;
    }
};

98. Validate Binary Search Tree

1.(*)

class Solution {
private:
    TreeNode* pre = NULL;
public:
    bool isValidBST(TreeNode* root) {
        if(root == NULL) return true;
        bool left = isValidBST(root->left);
        if(pre != NULL && pre->val >= root->val) return false;
        pre = root;
        bool right = isValidBST(root->right);
        return left&&right;
    }
};

2.(OK)

class Solution {
private:
    vector res;
    void traversal(TreeNode* root){
        if(root == NULL) return;
        traversal(root->left);
        res.push_back(root->val);
        traversal(root->right);
    }
public:
    bool isValidBST(TreeNode* root) {
        traversal(root);

        for(int i=1; i< res.size(); i++){
            if(res[i] <= res[i-1]) return false;
        }
        return true;
    }
};

你可能感兴趣的:(leetcode,算法,职场和发展)