File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
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
+
1
55
/**
2
56
* Definition for singly-linked list.
3
57
* function ListNode(val, next) {
You can’t perform that action at this time.
0 commit comments