leetcode 492-543 easy

492. Construct the Rectangle

Input: 4
Output: [2, 2]
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1]. 
But according to requirement 2, [1,4] is illegal; according to requirement 3,  [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
//[L,W]  L要大于W,所以以W为基础,设W为一半迭代下去
//1.Because question requires L, D as close as possible, I start the finding from the middle point which is sqrt(area). //2.when the Area divide Width have remainder 0, it should be the solution vector<int> constructRectangle(int area) { for(int mid = sqrt(area); mid>0; mid--) if (!(area%mid)) return {area/mid, mid}; }

 

501. Find Mode in Binary Search Tree

BST众数

 思路:由小到大inorder,O(n) time and O(1) space by inorder traversal

 

class Solution {
public:
    int maxFreq = 0, currFreq = 0, precursor = INT_MIN;
    vector<int> res;

    vector<int> findMode(TreeNode *root)
    {
        inorderTraversal(root);
        return res;
    }

    void inorderTraversal(TreeNode *root)
    {
        if (root == NULL) return; // Stop condition
        inorderTraversal(root->left); // Traverse left subtree
        if (precursor == root->val) currFreq++;
        else currFreq = 1;
        if (currFreq > maxFreq)
        {// Current node value has higher frequency than any previous visited
            res.clear();
            maxFreq = currFreq;
            res.push_back(root->val);
        }
        else if (currFreq == maxFreq)
        {// Current node value has a frequency equal to the highest of previous visited
            res.push_back(root->val);
        }
        precursor = root->val; // Update the precursor
        inorderTraversal(root->right); // Traverse right subtree
    }
};

 

 

 

520. Detect Capital

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".
class Solution(object):
    def detectCapitalUse(self, word):
        c = 0
        for i in word:  //统计大字母
            if i == i.upper():
                c += 1
        return c == len(word) or (c == 1 and word[0] == word[0].upper()) or c == 0   ##三种情况,随便一种都行,全部为大/只有头字母为大/全部为小

 

 

530. Minimum Absolute Difference in BST

小到大树,从左下角开始递归,每次算当前节点的值减去前一个节点的值,根据该值来更新min

void inorderTraverse(TreeNode* root, int& val, int& min_dif) {
    if (root->left != NULL) inorderTraverse(root->left, val, min_dif);
    if (val >= 0) min_dif = min(min_dif, root->val - val);
    val = root->val;
    if (root->right != NULL) inorderTraverse(root->right, val, min_dif);
}
int getMinimumDifference(TreeNode* root) {
    auto min_dif = INT_MAX, val = -1;
    inorderTraverse(root, val, min_dif);
    return min_dif;
}

 

 

538. Convert BST to Greater Tree

右边开始dfs,利用二叉树的右边比左边大的性质,递归加上右边的值

class Solution {
private:
    int cur_sum = 0;
public:
    void travel(TreeNode* root){
        if (!root) return;
        if (root->right) travel(root->right);
        
        root->val = (cur_sum += root->val);
        if (root->left) travel(root->left);
    }
    TreeNode* convertBST(TreeNode* root) {
        travel(root);
        return root;
    }
};

 

543. Diameter of Binary Tree

找最长的路径,不一定包含头节点,可以是从左下到右下

 

class Solution {
public:
int diameterOfBinaryTree(TreeNode* root) {
    if(root == nullptr) return 0;
    int res = depth(root->left) + depth(root->right);
    return max(res, max(diameterOfBinaryTree(root->left), diameterOfBinaryTree(root->right)));  //左右两个红框
}

int depth(TreeNode* root){
    if(root == nullptr) return 0;
    return 1 + max(depth(root->left), depth(root->right)); //黑框
}
};

 

posted @ 2018-11-23 11:10  热之雪  阅读(172)  评论(0编辑  收藏  举报