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 , it should be the solution vector<int> constructRectangle(int area) {
for(int mid = sqrt(area); mid>; 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 = , currFreq = , 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 = ;
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 =
for i in word: //统计大字母
if i == i.upper():
c +=
return c == len(word) or (c == and word[] == word[].upper()) or c == ##三种情况,随便一种都行,全部为大/只有头字母为大/全部为小

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 >= ) 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 = -;
inorderTraverse(root, val, min_dif);
return min_dif;
}

538. Convert BST to Greater Tree

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

class Solution {
private:
int cur_sum = ;
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 ;
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 ;
return + max(depth(root->left), depth(root->right)); //黑框
}
};

最新文章

  1. 利用QJSON将FDQuery转成JSON串
  2. business knowledge
  3. Unity浅析
  4. Unity3D 事件
  5. JAVA手记 JAVA入门(安装+Dos下运行)
  6. AT&amp;T ASSEMBLY FOR LINUX AND MAC (SYS_FORK)
  7. &amp;10 基本数据结构——指针和对象的实现,有根树的表示
  8. diff 文件比较
  9. 杭电2034——人见人爱A-B
  10. AS3 Signals
  11. CAS认证(2):认证过程
  12. 手动加入PE文件数字签名信息及格式具体解释图之下(历史代码,贴出学习)
  13. Dede推荐文章与热点文章不显示?
  14. web存储之webstorage
  15. HAProxy+Nginx 负载均衡
  16. 【Android Studio安装部署系列】二十九、Android Studio安装本地插件(以国际化方法插件AndroidLocalizationer为例)
  17. [ASP.NET] 如何利用Javascript分割檔案上傳至後端合併
  18. SQL 经典应用
  19. Dart server side call dll
  20. DBA角色职责

热门文章

  1. 设置Hadoop+Hbase集群pid文件存储位置
  2. Ubuntu环境下Error: Invalid or corrupt jarfile xxx.jar
  3. Nand Flash 控制器中的硬件 ECC 介绍
  4. 几道关于this的经典练习题的理解与分析
  5. DOM节点克隆
  6. Java中Arrys数组常用的方法
  7. task code
  8. leetcode-80-删除排序数组中的重复项②
  9. cycloneii normal mode vs. arithmetic mode
  10. php链表笔记:链表的检测