Learn More →
Learn More →
Learn More →
Learn More →
GIF太大傳不上來QQ
稍微畫個拓樸排序 等等可以對照
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
Learn More →
把7-6改成無限、把第7欄刪掉
因為第6列有一個0了,所以不用縮減
Learn More →
Learn More →
Learn More →
Learn More →
把5-3改成無限、把第5欄刪掉
因為第5列有一個0了,所以不用縮減
Learn More →
Learn More →
每個的lower bound
Learn More →
左右子樹的結點都翻一次
整棵樹就翻過來了
寫個遞迴給他左右翻轉
蠻簡單的
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root == NULL)
{
return NULL;
}
TreeNode* L = new TreeNode();
L = root->left;
TreeNode* R = new TreeNode();
R = root->right;
if(L!=NULL)
{
invertTree(L);
}
if(R!=NULL)
{
invertTree(R);
}
root->left=R;
root->right=L;
return root;
}
};
Learn More →
9分鐘
完全自己解出
因為二元搜尋樹的中序就是從小排到大
中序跑一次就好,全域變數紀錄跑到第幾個這樣
class Solution {
public:
int num = 0,ans = 0;
int kthSmallest(TreeNode* root, int k) {
num = 0;
ans = 0;
f(root,k);
return ans;
}
void f(TreeNode* root, int k) {
if(root == NULL)
{
return;
}
f(root->left,k);
num++;
if(num == k)
{
ans = root->val;
return;
}
if(num > k)
{
return;
}
f(root->right,k);
}
};
5分鐘
完全自己解出
紀錄目前遇到的最深的節點
如果遇到更深的,就把LD
更新、把sum歸零,並把點的數值加到sum
如果遇到跟最深的一樣深,就把點的數值加到sum
class Solution {
public:
int LD = -1;
int sum = 0;
int deepestLeavesSum(TreeNode* root) {
LD = -1;
sum = 0;
f(root , 0);
return sum;
}
void f(TreeNode* root,int d)
{
if(root == NULL)
{
return;
}
if(d > LD)
{
sum = root->val;
LD = d;
}
else if( d == LD )
{
sum += root->val;
}
f(root->left,d+1);
f(root->right,d+1);
}
};
10分鐘
完全自己解出
已填
該題求最長遞增子序列(Longest Increasing Subsequence)的長度。
Jun 7, 2024完全自己解出
May 30, 2024留言建議修訂請以 prune and search 解 selection problem。在以下 25 個數字中,要找出第 14 小的數。25 個數字依序為:15,18,12,10,12,4,6,14,10,1,3,5,1,5,20,3,4,10,7,5,1,15,2,4,8。分組方式為:第一組為第 1 到 5 個數字,第二組為第 6 到 10 個,依此類推。(A)請問第一回合找到的 p 值為多少?(B)請問第一回合,被刪去幾個數字?刪去的數字為何?
May 27, 2024image
May 20, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up