diff --git a/Linked List/Medium/1367.Linked List in Binary Tree b/Linked List/Medium/1367.Linked List in Binary Tree new file mode 100644 index 0000000..9d54652 --- /dev/null +++ b/Linked List/Medium/1367.Linked List in Binary Tree @@ -0,0 +1,73 @@ +QUESTION : +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: +1 <= node.val <= 100 for each node in the linked list and binary tree. +The given linked list will contain between 1 and 100 nodes. +The given binary tree will contain between 1 and 2500 nodes. + +C++ SOLUTION : + +class Solution { +public: + bool isSubPath(ListNode* head, TreeNode* root) { + if(root){ + if(isEqual(head, root)){ + return true; + } + return isSubPath(head, root->left) || isSubPath(head, root->right); + } + return false; + } + bool isEqual(ListNode* head, TreeNode* root){ + if(!head){ + return true; + } + if(!root){ + return false; + } + return head->val == root->val && (isEqual(head->next, root->left) || isEqual(head->next, root->right)); + } +}; + +PYTHON SOLUTION : + +def isSubPath(self, head: ListNode, root: TreeNode) -> bool: + target = "" + while head: + target = target + str(head.val) + head = head.next + + def dfs(root, path): + if target in path: + return True + + if root.left: + ans = dfs(root.left, path + str(root.left.val)) + if ans == True: + return True + + if root.right: + ans = dfs(root.right, path + str(root.right.val)) + if ans == True: + return True + + return False + + return dfs(root, str(root.val)) diff --git a/README.md b/README.md index 79c670f..3132639 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,16 @@ # LeetCode -* 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。
-* [我的博客](http://blog.csdn.net/c406495762 "悬停显示")
-* 我的个人网站:[http://cuijiahua.com/](http://cuijiahua.com/ "悬停显示")
-* 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! -
Coder
+ +原创文章每周最少两篇,**后续最新文章**会在[【公众号】](https://cuijiahua.com/wp-content/uploads/2020/05/gzh-w.jpg)首发,视频[【B站】](https://space.bilibili.com/331507846)首发,大家可以加我[【微信】](https://cuijiahua.com/wp-content/uploads/2020/05/gzh-w.jpg)进**交流群**,技术交流或提意见都可以,欢迎**Star**! + +

+ 微信群 + 公众号 + B站 + 知乎 + CSDN + 头条 + 掘金 +

## 帮助文档 @@ -98,7 +105,7 @@ | 290 | Easy | Word Pattern.mde | [Python](https://github.com/Jack-Cherish/LeetCode/blob/master/Hash%20Table/Easy/290.Word%20Pattern.md "悬停显示") | no | [思路讲解](https://github.com/Jack-Cherish/LeetCode/blob/master/Hash%20Table/Easy/290.Word%20Pattern.md "悬停显示") | # 剑指Offer(已完成) -* 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。
+ * [个人网站](http://cuijiahua.com/ "悬停显示")
### 链表(8道): @@ -192,3 +199,9 @@ * [剑指Offer(五十四):字符流中第一个不重复的字符](http://cuijiahua.com/blog/2018/01/basis_54.html "悬停显示") * [剑指Offer(六十三):数据流中的中位数](http://cuijiahua.com/blog/2018/02/basis_63.html "悬停显示") * [剑指Offer(六十四):滑动窗口的最大值](http://cuijiahua.com/blog/2018/02/basis_64.html "悬停显示") + +更多精彩,敬请期待! + + + +wechat