/**
 * 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 {
    ListNode ll;

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

    private boolean solve(ListNode head, TreeNode root) {
        if (head == null)
            return true;
        if (root == null)
            return false;
        // System.out.println(head.val + "\t" + root.val);
        if (root.val != head.val)
            return solve(ll, root.left) || solve(ll, root.right);
        return solve(head.next, root.left) || solve(head.next, root.right);
    }
}