Skip to content

Commit ad7fb9d

Browse files
committed
Create 1367.二叉树中的列表.js
1 parent b196f93 commit ad7fb9d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

1367.二叉树中的列表.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val) {
11+
* this.val = val;
12+
* this.left = this.right = null;
13+
* }
14+
*/
15+
/**
16+
* @param {ListNode} head
17+
* @param {TreeNode} root
18+
* @return {boolean}
19+
*/
20+
var isSubPath = function(head, root) {
21+
if (!head) {
22+
return true;
23+
}
24+
if (!root) {
25+
return false;
26+
}
27+
28+
function dfs(lNode, node) {
29+
if (!lNode) {
30+
return true;
31+
}
32+
if (!node) {
33+
return false;
34+
}
35+
if (lNode.val !== node.val) {
36+
return false;
37+
}
38+
return dfs(lNode.next, node.left) || dfs(lNode.next, node.right);
39+
}
40+
41+
return dfs(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
42+
};

0 commit comments

Comments
 (0)