Linked List in Binary Tree LeetCode solution in Java

1
class Solution {
2
public boolean isSubPath(ListNode head, TreeNode root) {
3
if (root == null) return false;
4
if (issame(head, root)) return true;
5
return isSubPath(head, root.left) || isSubPath(head, root.right);
6
}
7

8
private boolean issame(ListNode head, TreeNode root) {
9
if (head == null) return true;
10
if (root == null) return false;
11
if (head.val != root.val) return false;
12
return issame(head.next, root.left) || issame(head.next, root.right);
13
}
14
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0