Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RakhmedovRS/db8111d9d89a0ba2d910f5ef37036290 to your computer and use it in GitHub Desktop.
Save RakhmedovRS/db8111d9d89a0ba2d910f5ef37036290 to your computer and use it in GitHub Desktop.
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