Created
July 2, 2022 21:27
-
-
Save RakhmedovRS/db8111d9d89a0ba2d910f5ef37036290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isSubPath(ListNode head, TreeNode root) | |
{ | |
return traverse(root, head); | |
} | |
private boolean traverse(TreeNode root, ListNode head) | |
{ | |
if (root == null) | |
{ | |
return head == null; | |
} | |
if (root.val == head.val && findPath(root, head)) | |
{ | |
return true; | |
} | |
return traverse(root.left, head) || traverse(root.right, head); | |
} | |
private boolean findPath(TreeNode root, ListNode head) | |
{ | |
if (head == null) | |
{ | |
return true; | |
} | |
if (root == null) | |
{ | |
return false; | |
} | |
return root.val == head.val && (findPath(root.left, head.next) || findPath(root.right, head.next)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment