Re: [閒聊] 每日leetcode

作者: argorok (s.green)   2024-09-07 14:20:16
※ 引述《dont (dont)》之銘言:
: 1367. Linked List in Binary Tree
: ## 思路
preorder 遍歷整個tree, 每個node都recursion檢查一下是否有path
class Solution {
public:
bool check(ListNode* head, TreeNode* root) {
if(head == nullptr) return true;
if(root == nullptr || head->val != root->val) return false;
head = head->next;
return check(head, root->left) || check(head, root->right);
}
bool isSubPath(ListNode* head, TreeNode* root) {
if(root == nullptr) return false;
if(check(head, root)) return true;
return isSubPath(head, root->left) || isSubPath(head, root->right);
}
};
作者: dont   2024-09-07 14:24:00
大師
作者: DJYOMIYAHINA (通通打死)   2024-09-07 14:24:00
大師
作者: argorok (s.green)   2024-09-07 14:25:00
刷到神智錯亂 改一下==
作者: sustainer123 (caster)   2024-09-07 14:30:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com