forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1367. Linked List in Binary Tree
- Loading branch information
Showing
4 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
solution/1300-1399/1367.Linked List in Binary Tree/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
solution/1300-1399/1367.Linked List in Binary Tree/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |