Skip to content

Commit f16a572

Browse files
authored
Update 1367-linked-list-in-binary-tree.js
1 parent 7d586db commit f16a572

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

1367-linked-list-in-binary-tree.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {ListNode} head
18+
* @param {TreeNode} root
19+
* @return {boolean}
20+
*/
21+
const isSubPath = function(head, root) {
22+
return dfs(root)
23+
24+
function dfs(node) {
25+
if(node == null) return false
26+
if(head.val === node.val) {
27+
let cur = head
28+
let q = [node]
29+
while(q.length) {
30+
const v = cur.val
31+
const tmp = []
32+
let mark = false
33+
for(const e of q) {
34+
if(e.val === v) {
35+
mark = true
36+
if(e.left) tmp.push(e.left)
37+
if(e.right) tmp.push(e.right)
38+
}
39+
}
40+
if(cur && !mark) break
41+
cur = cur.next
42+
if(cur == null) return true
43+
q = tmp
44+
}
45+
}
46+
return dfs(node.left) || dfs(node.right)
47+
}
48+
49+
};
50+
51+
52+
// another
53+
54+
155
/**
256
* Definition for singly-linked list.
357
* function ListNode(val, next) {

0 commit comments

Comments
 (0)