From c304aaee3be8f7c661f8359d92d929fa4dc61a06 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 26 May 2021 21:05:01 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1367. Linked List in Binary Tree --- .../1367.Linked List in Binary Tree/README.md | 74 ++++++++++++++++++- .../README_EN.md | 74 ++++++++++++++++++- .../Solution.java | 46 ++++++++++++ .../Solution.py | 25 +++++++ 4 files changed, 213 insertions(+), 6 deletions(-) create mode 100644 solution/1300-1399/1367.Linked List in Binary Tree/Solution.java create mode 100644 solution/1300-1399/1367.Linked List in Binary Tree/Solution.py diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/README.md b/solution/1300-1399/1367.Linked List in Binary Tree/README.md index 52147706e1416..a594c6b5277d0 100644 --- a/solution/1300-1399/1367.Linked List in Binary Tree/README.md +++ b/solution/1300-1399/1367.Linked List in Binary Tree/README.md @@ -48,7 +48,6 @@
  • 二叉树包含的节点数目在 1 到 2500 之间。
  • - ## 解法 @@ -60,7 +59,31 @@ ```python - +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSubPath(self, head: ListNode, root: TreeNode) -> bool: + def dfs(head, root): + if head is None: + return True + if root is None: + return False + if root.val != head.val: + return False + return dfs(head.next, root.left) or dfs(head.next, root.right) + + if root is None: + return False + return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right) ``` ### **Java** @@ -68,7 +91,52 @@ ```java - +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSubPath(ListNode head, TreeNode root) { + if (root == null) { + return false; + } + return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right); + } + + private boolean dfs(ListNode head, TreeNode root) { + if (head == null) { + return true; + } + if (root == null) { + return false; + } + if (root.val != head.val) { + return false; + } + return dfs(head.next, root.left) || dfs(head.next, root.right); + } +} ``` ### **...** diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md b/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md index 344addce61a97..8df4ea5017437 100644 --- a/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md +++ b/solution/1300-1399/1367.Linked List in Binary Tree/README_EN.md @@ -47,7 +47,6 @@
  • 1 <= Node.val <= 100 for each node in the linked list and binary tree.
  • - ## Solutions @@ -55,13 +54,82 @@ ### **Python3** ```python - +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSubPath(self, head: ListNode, root: TreeNode) -> bool: + def dfs(head, root): + if head is None: + return True + if root is None: + return False + if root.val != head.val: + return False + return dfs(head.next, root.left) or dfs(head.next, root.right) + + if root is None: + return False + return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right) ``` ### **Java** ```java - +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSubPath(ListNode head, TreeNode root) { + if (root == null) { + return false; + } + return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right); + } + + private boolean dfs(ListNode head, TreeNode root) { + if (head == null) { + return true; + } + if (root == null) { + return false; + } + if (root.val != head.val) { + return false; + } + return dfs(head.next, root.left) || dfs(head.next, root.right); + } +} ``` ### **...** diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/Solution.java b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.java new file mode 100644 index 0000000000000..64b27d340f815 --- /dev/null +++ b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.java @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSubPath(ListNode head, TreeNode root) { + if (root == null) { + return false; + } + return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right); + } + + private boolean dfs(ListNode head, TreeNode root) { + if (head == null) { + return true; + } + if (root == null) { + return false; + } + if (root.val != head.val) { + return false; + } + return dfs(head.next, root.left) || dfs(head.next, root.right); + } +} \ No newline at end of file diff --git a/solution/1300-1399/1367.Linked List in Binary Tree/Solution.py b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.py new file mode 100644 index 0000000000000..7f6968199337b --- /dev/null +++ b/solution/1300-1399/1367.Linked List in Binary Tree/Solution.py @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSubPath(self, head: ListNode, root: TreeNode) -> bool: + def dfs(head, root): + if head is None: + return True + if root is None: + return False + if root.val != head.val: + return False + return dfs(head.next, root.left) or dfs(head.next, root.right) + + if root is None: + return False + return dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)