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