<script>
class listNode
{
constructor(data) {
this
.val = data;
this
.next =
null
;
}
}
class treeNode
{
constructor(data) {
this
.val = data;
this
.left =
null
;
this
.right =
null
;
}
}
function
isPathUtil(curList, curTree)
{
if
(curList ==
null
)
return
true
;
if
(curTree ==
null
)
return
false
;
if
(curList.val == curTree.val)
{
return
(
isPathUtil(curList.next, curTree.left) ||
isPathUtil(curList.next, curTree.right)
);
}
else
{
return
false
;
}
}
function
isPath(head, root)
{
if
(root ==
null
)
return
false
;
if
(head ==
null
)
return
true
;
let isPossible =
false
;
if
(root.val == head.val) {
isPossible =
isPathUtil(head.next, root.left) || isPathUtil(head.next, root.right);
}
return
isPossible || isPath(head, root.left) || isPath(head, root.right);
}
let root =
new
treeNode(1);
root.left =
new
treeNode(2);
root.right =
new
treeNode(3);
root.left.left =
new
treeNode(4);
root.left.right =
new
treeNode(5);
root.left.right.left =
new
treeNode(7);
root.right.right =
new
treeNode(6);
root.right.right.left =
new
treeNode(8);
root.right.right.right =
new
treeNode(9);
let head =
new
listNode(3);
head.next =
new
listNode(6);
head.next.next =
new
listNode(8);
document.write(isPath(head, root) ?
"Yes"
:
"No"
);
</script>