package leetcode;

/**
 * https://leetcode.com/problems/linked-list-in-binary-tree/
 */
public class Problem1367 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public boolean isSubPath(ListNode head, TreeNode root) {
        if (root == null) {
            return false;
        }
        boolean answer = false;
        if (head.val == root.val) {
            answer |= subPath(head, root);
        }
        answer |= subPath(head, root.left);
        answer |= subPath(head, root.right);
        return answer;
    }

    private static boolean subPath(ListNode head, TreeNode root) {
        if (head == null) {
            return true;
        }
        if (root == null || head.val != root.val) {
            return false;
        }
        return subPath(head.next, root.left) || subPath(head.next, root.right);
    }

    public static void main(String[] args) {
        Problem1367 prob = new Problem1367();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(4);
        root.left.right = new TreeNode(2);
        root.left.right.left = new TreeNode(1);
        root.right = new TreeNode(4);
        root.right.left = new TreeNode(2);
        root.right.left.left = new TreeNode(6);
        root.right.left.right = new TreeNode(8);
        root.right.left.right.left = new TreeNode(1);
        root.right.left.right.right = new TreeNode(3);

        ListNode head = new ListNode(4);
        head.next = new ListNode(2);
        head.next.next = new ListNode(8);

        System.out.println(prob.isSubPath(head, root)); // true
    }
}

@@ -668,33 +668,35 @@
       answer %7C= 
-s
+isS
 ubPath(head, roo
@@ -714,33 +714,35 @@
       answer %7C= 
-s
+isS
 ubPath(head, roo
@@ -991,32 +991,32 @@
 alse;%0A        %7D%0A
+
         return s
@@ -1088,748 +1088,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1367 prob = new Problem1367();%0A%0A        TreeNode root = new TreeNode(1);%0A        root.left = new TreeNode(4);%0A        root.left.right = new TreeNode(2);%0A        root.left.right.left = new TreeNode(1);%0A        root.right = new TreeNode(4);%0A        root.right.left = new TreeNode(2);%0A        root.right.left.left = new TreeNode(6);%0A        root.right.left.right = new TreeNode(8);%0A        root.right.left.right.left = new TreeNode(1);%0A        root.right.left.right.right = new TreeNode(3);%0A%0A        ListNode head = new ListNode(4);%0A        head.next = new ListNode(2);%0A        head.next.next = new ListNode(8);%0A%0A        System.out.println(prob.isSubPath(head, root)); // true%0A    %7D
 %0A%7D%0A

Solve problem 1367