2024年7月25日 星期四

[1367] Linked List in Binary Tree (TBD)

感覺有點tricky的題目?!(thinking圖)
不太確定自己能不能寫出來
但是已經看解答了,那就先來個TBD吧XD 
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool isPath(struct ListNode* head, struct TreeNode* root) {
if (head == NULL)
return true;
if (root == NULL)
return false;
#if 1
if (root->val == head->val)
return (isPath(head->next, root->left) || isPath(head->next, root->right));
else
return false;
#else
return ((root->val == head->val) &&
(isPath(head->next, root->left) | isPath(head->next, root->right)));
#endif}

bool isSubPath(struct ListNode* head, struct TreeNode* root) {
if (root == NULL)
return false;
return isPath(head,root) || isSubPath(head, root->left) || isSubPath(head, root->right);
}

沒有留言:

張貼留言