- Linked List in Binary Tree
Medium
Given a binary tree root and a linked list with head as the first node.
Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.
In this context downward path means a path that starts at some node and goes downwards.
Example 1:
Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Explanation: Nodes in blue form a subpath in the binary Tree.
Example 2:
Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Example 3:
Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: false
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.
Constraints:
The number of nodes in the tree will be in the range [1, 2500].
The number of nodes in the list will be in the range [1, 100].
1 <= Node.val <= 100 for each node in the linked list and binary tree.
解法1:
這題其實(shí)并不容易。要在整個(gè)二叉樹里面,對每個(gè)節(jié)點(diǎn)調(diào)用helper()函數(shù),用前中后序遍歷應(yīng)該都可以。helper()則是用的分解問題的方法。文章來源:http://www.zghlxwxcb.cn/news/detail-822298.html
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSubPath(ListNode* head, TreeNode* root) {
if (!head) return true;
if (!root) return false;
if (helper(head, root)) return true;
return isSubPath(head, root->left) || isSubPath(head, root->right);
}
private:
bool helper(ListNode* head, TreeNode* root) {
if (!head) return true;
if (!root) return false;
if (head->val != root->val) return false;
return helper(head->next, root->left) || helper(head->next, root->right);
}
};
我一開始寫成下面這樣,但是不對。因?yàn)樗徽{(diào)用了一次helper(),如果鏈表是{2,2,1},而樹里面存在一個(gè)path{2,2,2,1},結(jié)果應(yīng)該返回true。但這個(gè)解法里面,讀到第3個(gè)2的時(shí)候,發(fā)現(xiàn)不對,已經(jīng)沒法走回頭路了。
應(yīng)該在整個(gè)樹里面,對每個(gè)節(jié)點(diǎn)都調(diào)用helper(),用前/中/后序遍歷都可以。文章來源地址http://www.zghlxwxcb.cn/news/detail-822298.html
class Solution {
public:
bool isSubPath(ListNode* head, TreeNode* root) {
origHead = head;
origRoot = root;
helper(head, root);
return gPathExist;
}
private:
bool gPathExist = false;
TreeNode *origRoot = NULL;
ListNode *origHead = NULL;
void helper(ListNode *head, TreeNode *root) {
if (!head) {
gPathExist = true;
return;
}
if (!root || gPathExist) return;
if (root->val == head->val) {
helper(head->next, root->left);
helper(head->next, root->right);
} else {
if (root == origRoot) {
helper(origHead, root->left);
helper(origHead, root->right);
} else if (head != origHead) {
helper(origHead, root);
helper(origHead, root);
}
}
}
};
到了這里,關(guān)于Leetcode 1367. Linked List in Binary Tree (二叉樹好題)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!