1367
- 哪个地方是起点?请递归?
- 哪条路径是对的,请递归一个个找
class Solution {
public:
bool isSubPath(ListNode* head, TreeNode* root) {
if(root== nullptr) return false;
return isSub(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
}
bool isSub(ListNode* head, TreeNode* root){
if(head == nullptr) return true;
if(root == nullptr) return false;
if(head->val != root->val) return false;
return isSub(head->next, root->left) || isSub(head->next, root->right);
}
};
return isSub(head, root) || isSubPath(head, root->left) || isSubPath(head, root->right);
这个是看与它连的左右子树。
return isSub(head->next, root->left) || isSub(head->next, root->right);
这个看每每几个点去作对比。
1171.
- 不需要往回作追溯,就算中间一段==0,于总和+0没有影响。
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
ListNode* dummyHead = new ListNode;
dummyHead->next = head;
ListNode* prev = dummyHead;
ListNode* start = head;
ListNode* end;
while(start){
end = start;
int sum = end->val;
while(end){
if(sum==0){//
prev->next = end->next; //一旦删去,回到原点?
start = dummyHead->next;
break;
}
sum += end->val;
end = end->next;
}
prev = start;
start = start->next;
}
return dummyHead->next;
}
};
以上求解错了??
class Solution {
public ListNode removeZeroSumSublists(ListNode head) {
ListNode p = new ListNode(0);
ListNode pre = p;
p.next = head;
while(p!=null){
ListNode cur = p.next;
int sum=0;
while(cur!=null){
sum+=cur.val;
cur=cur.next;
if(sum==0){
p.next =cur;
break;
}
}
if(cur==null) p=p.next;
}
return pre.next;
}
}
//差一点??
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* start = dummyHead;
while(start){
ListNode* end = start->next; //愉快省去了一个prev量
int sum = 0;
while(end){
sum += end->val; //这样才是真正从第一个开始算
end = end->next;
if(sum==0){//
start->next = end; //一旦删去,回到原点?
break;
}
}
start = start->next;
}
return dummyHead->next;
}
};
114.
解题的关键是:左子树的最下最右的节点,是右子树的父节点.
这个错了??
class Solution {
public:
void flatten(TreeNode* root) {
TreeNode* dummy = new TreeNode;
dfs(root, dummy);
root = dummy;
}
void dfs(TreeNode* root, TreeNode* prev){
if(root == nullptr) return;
prev->left = nullptr;
prev->right = root;
dfs(root->left, root);
dfs(root->right, root);
}
};
100.
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q){
if(!p && !q) return true;
if(!p || !q || (p->val != q->val)) return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
222
这个办法耗时比较长???
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == nullptr) return 0;
stack<TreeNode*> stk;
stk.push(root);
int ans = 0;
while(!stk.empty()){
int n = stk.size();
ans += n;
for(int i=0; i< n; i++){
TreeNode* cur = stk.top();
stk.pop();
if(cur->left) stk.push(cur->left);
if(cur->right) stk.push(cur->right);
}
}
return ans;
}
};
//该迭代法更加迅速???
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == nullptr) return 0;
int left = countNodes(root->left);
int right = countNodes(root->right);
return left+right+1;
}
};
230.
- 这里的count是一旦??触发器,碰到最左边开始计数
- 不去想什么序的遍历,left走过之后就要作判断!
class Solution {
public:
int count = 0; //同时也起标志位作用
int ans = 0;
int kthSmallest(TreeNode* root, int k) {
dfs(root, k);
return ans;
}
void dfs(TreeNode* root, int k){
if(root== nullptr) return;
dfs(root->left, k);
if(root->left== nullptr || count != 0) count++;
if(count == k){
ans = root->val;
}
dfs(root->right, k);
}
};
671.
理解树从底部向上的这一过程!?
class Solution {
public:
int ans = INT_MAX;
bool flag = false;
int findSecondMinimumValue(TreeNode* root) {
dfs(root);
if(flag) return ans;
else return -1;
}
void dfs(TreeNode* root){
if(root == nullptr) return ;
dfs(root->left);
dfs(root->right);
if(root->left && root->right && (root->left->val != root->right->val)){
int tmp = max(root->left->val, root->right->val);
ans = min(ans, tmp);
flag = true;
}
}
};
508.
骄傲,90%完胜!!
class Solution {
public:
vector<int> ans;
unordered_map<int, int> dict;
vector<int> findFrequentTreeSum(TreeNode* root) {
dfs(root);
int ctn = 1;
for(auto a: dict){
ctn = max(ctn, a.second);
}
for(auto a: dict){
if(a.second == ctn) ans.push_back(a.first);
}
return ans;
}
int dfs(TreeNode* root){
if(root==nullptr){
return 0;
}
int l = dfs(root->left);
int r = dfs(root->right);
int sum = l+r+ root->val;
dict[sum] ++;
return sum;
}
};
572.
150/180,还有没通过???
class Solution {
public:
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if(root == nullptr) return false;
return isSub(root, subRoot) || isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
bool isSub(TreeNode* root, TreeNode* subRoot){
if(!root && !subRoot) return true;
if(!root || !subRoot || (root->val != subRoot->val)){
return false;
}
return isSub(root->left, subRoot->left) && isSub(root->right, root->right);
}
};
正解??
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
if(s==nullptr && t==nullptr) return true;
if(s == nullptr || t== nullptr) return false;
return isSameTree(s,t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
bool isSameTree(TreeNode* s, TreeNode* t){
if(s==nullptr && t==nullptr) return true;
if(s == nullptr || t== nullptr) return false;
return s->val == t->val && isSameTree(s->left, t->left) && isSameTree(s->right, t->right);
}
};
if(root==nullptr && subRoot==nullptr) return true;
if(root == nullptr || subRoot== nullptr) return false;
//if(root == nullptr) return false;
不是这个的问题??树枝没剪掉??