-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1367.二叉树中的列表.js
42 lines (40 loc) · 870 Bytes
/
1367.二叉树中的列表.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {ListNode} head
* @param {TreeNode} root
* @return {boolean}
*/
var isSubPath = function(head, root) {
if (!head) {
return true;
}
if (!root) {
return false;
}
function dfs(lNode, node) {
if (!lNode) {
return true;
}
if (!node) {
return false;
}
if (lNode.val !== node.val) {
return false;
}
return dfs(lNode.next, node.left) || dfs(lNode.next, node.right);
}
return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
};