1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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); }
public boolean dfs(ListNode head, TreeNode root){ if(head == null){ return true; }
if(root == null){ return false; }
if(head.val != root.val){ return false; } return dfs(head.next, root.left) || dfs(head.next, root.right); } }
|