Re: [閒聊] 每日leetcode
※ 引述《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);
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.98.88 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725690018.A.A86.html
推
09/07 14:24,
1年前
, 1F
09/07 14:24, 1F
推
09/07 14:24,
1年前
, 2F
09/07 14:24, 2F
※ 編輯: argorok (36.228.98.88 臺灣), 09/07/2024 14:25:31
→
09/07 14:25,
1年前
, 3F
09/07 14:25, 3F
推
09/07 14:30,
1年前
, 4F
09/07 14:30, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 822 之 1548 篇):