|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.ListNode; |
| 4 | +import com.fishercoder.common.classes.TreeNode; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** |
| 10 | + * 1367. Linked List in Binary Tree |
| 11 | + * |
| 12 | + * Given a binary tree root and a linked list with head as the first node. |
| 13 | + * Return True if all the elements in the linked list starting from the head correspond to some downward path |
| 14 | + * connected in the binary tree otherwise return False. |
| 15 | + * In this context downward path means a path that starts at some node and goes downwards. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] |
| 19 | + * Output: true |
| 20 | + * Explanation: Nodes in blue form a subpath in the binary Tree. |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] |
| 24 | + * Output: true |
| 25 | + * |
| 26 | + * Example 3: |
| 27 | + * 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] |
| 28 | + * Output: false |
| 29 | + * Explanation: There is no path in the binary tree that contains all the elements of the linked list from head. |
| 30 | + * |
| 31 | + * Constraints: |
| 32 | + * 1 <= node.val <= 100 for each node in the linked list and binary tree. |
| 33 | + * The given linked list will contain between 1 and 100 nodes. |
| 34 | + * The given binary tree will contain between 1 and 2500 nodes. |
| 35 | + * */ |
| 36 | +public class _1367 { |
| 37 | + public static class Solution1 { |
| 38 | + List<List<Integer>> paths = new ArrayList<>(); |
| 39 | + |
| 40 | + public boolean isSubPath(ListNode head, TreeNode root) { |
| 41 | + List<Integer> list = getList(head); |
| 42 | + findAllPaths(root, new ArrayList<>()); |
| 43 | + for (List<Integer> path : paths) { |
| 44 | + if (path.size() >= list.size()) { |
| 45 | + if (find(list, path)) { |
| 46 | + return true; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + private boolean find(List<Integer> list, List<Integer> path) { |
| 54 | + int i = 0; |
| 55 | + int j = 0; |
| 56 | + for (; i <= path.size() - list.size(); i++) { |
| 57 | + j = 0; |
| 58 | + int tmpI = i; |
| 59 | + while (j < list.size() && tmpI < path.size() && list.get(j) == path.get(tmpI)) { |
| 60 | + tmpI++; |
| 61 | + j++; |
| 62 | + } |
| 63 | + if (j >= list.size()) { |
| 64 | + return true; |
| 65 | + } |
| 66 | + } |
| 67 | + return j >= list.size(); |
| 68 | + } |
| 69 | + |
| 70 | + private void findAllPaths(TreeNode root, List<Integer> path) { |
| 71 | + if (root == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + path.add(root.val); |
| 75 | + if (root.left == null && root.right == null) { |
| 76 | + paths.add(new ArrayList<>(path)); |
| 77 | + path.remove(path.size() - 1); |
| 78 | + return; |
| 79 | + } |
| 80 | + findAllPaths(root.left, path); |
| 81 | + findAllPaths(root.right, path); |
| 82 | + path.remove(path.size() - 1); |
| 83 | + } |
| 84 | + |
| 85 | + private List<Integer> getList(ListNode head) { |
| 86 | + List<Integer> list = new ArrayList<>(); |
| 87 | + while (head != null) { |
| 88 | + list.add(head.val); |
| 89 | + head = head.next; |
| 90 | + } |
| 91 | + return list; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments